2 * File...........: arch/s390/mm/extmem.c
3 * Author(s)......: Carsten Otte <cotte@de.ibm.com>
4 * Rob M van der Heij <rvdheij@nl.ibm.com>
5 * Steven Shultz <shultzss@us.ibm.com>
6 * Bugreports.to..: <Linux390@de.ibm.com>
7 * (C) IBM Corporation 2002-2004
10 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/bootmem.h>
17 #include <linux/ctype.h>
18 #include <linux/ioport.h>
20 #include <asm/pgtable.h>
21 #include <asm/ebcdic.h>
22 #include <asm/errno.h>
23 #include <asm/extmem.h>
24 #include <asm/cpcmd.h>
25 #include <asm/setup.h>
27 #define DCSS_DEBUG /* Debug messages on/off */
29 #define DCSS_NAME "extmem"
31 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
33 #define PRINT_DEBUG(x...) do {} while (0)
35 #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
36 #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
37 #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
40 #define DCSS_LOADSHR 0x00
41 #define DCSS_LOADNSR 0x04
42 #define DCSS_PURGESEG 0x08
43 #define DCSS_FINDSEG 0x0c
44 #define DCSS_LOADNOLY 0x10
45 #define DCSS_SEGEXT 0x18
46 #define DCSS_FINDSEGA 0x0c
49 unsigned int start; // 3byte start address, 1 byte type
50 unsigned int end; // 3byte end address, 1 byte reserved
58 struct qrange range[6];
72 struct list_head list;
75 unsigned long start_addr;
79 unsigned int vm_segtype;
80 struct qrange range[6];
85 static DEFINE_MUTEX(dcss_lock);
86 static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
87 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
91 * Create the 8 bytes, ebcdic VM segment name from
95 dcss_mkname(char *name, char *dcss_name)
99 for (i = 0; i < 8; i++) {
102 dcss_name[i] = toupper(name[i]);
106 ASCEBC(dcss_name, 8);
111 * search all segments in dcss_list, and return the one
112 * namend *name. If not found, return NULL.
114 static struct dcss_segment *
115 segment_by_name (char *name)
119 struct dcss_segment *tmp, *retval = NULL;
121 BUG_ON(!mutex_is_locked(&dcss_lock));
122 dcss_mkname (name, dcss_name);
123 list_for_each (l, &dcss_list) {
124 tmp = list_entry (l, struct dcss_segment, list);
125 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
135 * Perform a function on a dcss segment.
138 dcss_diag (__u8 func, void *parameter,
139 unsigned long *ret1, unsigned long *ret2)
141 unsigned long rx, ry;
144 rx = (unsigned long) parameter;
145 ry = (unsigned long) func;
156 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
163 dcss_diag_translate_rc (int vm_rc) {
170 /* do a diag to get info about a segment.
171 * fills start_address, end and vm_segtype fields
174 query_segment_type (struct dcss_segment *seg)
176 struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
177 struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
180 unsigned long dummy, vmrc;
182 if ((qin == NULL) || (qout == NULL)) {
187 /* initialize diag input parameters */
188 qin->qopcode = DCSS_FINDSEGA;
189 qin->qoutptr = (unsigned long) qout;
190 qin->qoutlen = sizeof(struct qout64);
191 memcpy (qin->qname, seg->dcss_name, 8);
193 diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
196 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
197 rc = dcss_diag_translate_rc (vmrc);
201 if (qout->segcnt > 6) {
206 if (qout->segcnt == 1) {
207 seg->vm_segtype = qout->range[0].start & 0xff;
209 /* multi-part segment. only one type supported here:
210 - all parts are contiguous
211 - all parts are either EW or EN type
212 - maximum 6 parts allowed */
213 unsigned long start = qout->segstart >> PAGE_SHIFT;
214 for (i=0; i<qout->segcnt; i++) {
215 if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
216 ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
220 if (start != qout->range[i].start >> PAGE_SHIFT) {
224 start = (qout->range[i].end >> PAGE_SHIFT) + 1;
226 seg->vm_segtype = SEG_TYPE_EWEN;
229 /* analyze diag output and update seg */
230 seg->start_addr = qout->segstart;
231 seg->end = qout->segend;
233 memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
234 seg->segcnt = qout->segcnt;
245 * get info about a segment
246 * possible return values:
247 * -ENOSYS : we are not running on VM
248 * -EIO : could not perform query diagnose
249 * -ENOENT : no such segment
250 * -ENOTSUPP: multi-part segment cannot be used with linux
251 * -ENOSPC : segment cannot be used (overlaps with storage)
252 * -ENOMEM : out of memory
253 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
256 segment_type (char* name)
259 struct dcss_segment seg;
264 dcss_mkname(name, seg.dcss_name);
265 rc = query_segment_type (&seg);
268 return seg.vm_segtype;
272 * real segment loading function, called from segment_load
275 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
277 struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
279 int dcss_command, rc, diag_cc;
285 dcss_mkname (name, seg->dcss_name);
286 rc = query_segment_type (seg);
290 rc = add_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
296 PRINT_WARN("segment_load: not loading segment %s - overlaps "
297 "storage/segment\n", name);
300 PRINT_WARN("segment_load: not loading segment %s - exceeds "
301 "kernel mapping range\n", name);
304 PRINT_WARN("segment_load: not loading segment %s (rc: %d)\n",
309 seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
310 if (seg->res == NULL) {
314 seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
315 seg->res->start = seg->start_addr;
316 seg->res->end = seg->end;
317 memcpy(&seg->res_name, seg->dcss_name, 8);
318 EBCASC(seg->res_name, 8);
319 seg->res_name[8] = '\0';
320 strncat(seg->res_name, " (DCSS)", 7);
321 seg->res->name = seg->res_name;
322 rc = seg->vm_segtype;
323 if (rc == SEG_TYPE_SC ||
324 ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
325 seg->res->flags |= IORESOURCE_READONLY;
326 if (request_resource(&iomem_resource, seg->res)) {
333 dcss_command = DCSS_LOADNSR;
335 dcss_command = DCSS_LOADNOLY;
337 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
338 &seg->start_addr, &seg->end);
340 PRINT_WARN ("segment_load: could not load segment %s - "
341 "diag returned error (%ld)\n",name,seg->end);
342 rc = dcss_diag_translate_rc (seg->end);
343 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
344 &seg->start_addr, &seg->end);
347 seg->do_nonshared = do_nonshared;
348 atomic_set(&seg->ref_count, 1);
349 list_add(&seg->list, &dcss_list);
350 *addr = seg->start_addr;
353 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
354 "type %s in non-shared mode\n", name,
355 (void*)seg->start_addr, (void*)seg->end,
356 segtype_string[seg->vm_segtype]);
358 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
359 "type %s in shared mode\n", name,
360 (void*)seg->start_addr, (void*)seg->end,
361 segtype_string[seg->vm_segtype]);
365 release_resource(seg->res);
368 remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
376 * this function loads a DCSS segment
377 * name : name of the DCSS
378 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
379 * 1 indicates that the dcss should be exclusive for this linux image
380 * addr : will be filled with start address of the segment
381 * end : will be filled with end address of the segment
383 * -ENOSYS : we are not running on VM
384 * -EIO : could not perform query or load diagnose
385 * -ENOENT : no such segment
386 * -ENOTSUPP: multi-part segment cannot be used with linux
387 * -ENOSPC : segment cannot be used (overlaps with storage)
388 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
389 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
390 * -EPERM : segment is currently loaded with incompatible permissions
391 * -ENOMEM : out of memory
392 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
395 segment_load (char *name, int do_nonshared, unsigned long *addr,
398 struct dcss_segment *seg;
404 mutex_lock(&dcss_lock);
405 seg = segment_by_name (name);
407 rc = __segment_load (name, do_nonshared, addr, end);
409 if (do_nonshared == seg->do_nonshared) {
410 atomic_inc(&seg->ref_count);
411 *addr = seg->start_addr;
413 rc = seg->vm_segtype;
419 mutex_unlock(&dcss_lock);
424 * this function modifies the shared state of a DCSS segment. note that
425 * name : name of the DCSS
426 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
427 * 1 indicates that the dcss should be exclusive for this linux image
429 * -EIO : could not perform load diagnose (segment gone!)
430 * -ENOENT : no such segment (segment gone!)
431 * -EAGAIN : segment is in use by other exploiters, try later
432 * -EINVAL : no segment with the given name is currently loaded - name invalid
433 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
434 * 0 : operation succeeded
437 segment_modify_shared (char *name, int do_nonshared)
439 struct dcss_segment *seg;
441 int dcss_command, rc, diag_cc;
443 mutex_lock(&dcss_lock);
444 seg = segment_by_name (name);
449 if (do_nonshared == seg->do_nonshared) {
450 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
451 " - already in requested mode\n",name);
455 if (atomic_read (&seg->ref_count) != 1) {
456 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
457 "segment is in use by other driver(s)\n",name);
461 release_resource(seg->res);
463 dcss_command = DCSS_LOADNSR;
464 seg->res->flags &= ~IORESOURCE_READONLY;
466 dcss_command = DCSS_LOADNOLY;
467 if (seg->vm_segtype == SEG_TYPE_SR ||
468 seg->vm_segtype == SEG_TYPE_ER)
469 seg->res->flags |= IORESOURCE_READONLY;
471 if (request_resource(&iomem_resource, seg->res)) {
472 PRINT_WARN("segment_modify_shared: could not reload segment %s"
473 " - overlapping resources\n", name);
478 dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
479 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
480 &seg->start_addr, &seg->end);
482 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
483 " - diag returned error (%ld)\n",name,seg->end);
484 rc = dcss_diag_translate_rc (seg->end);
487 seg->do_nonshared = do_nonshared;
491 remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
492 list_del(&seg->list);
493 dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
496 mutex_unlock(&dcss_lock);
501 * Decrease the use count of a DCSS segment and remove
502 * it from the address space if nobody is using it
506 segment_unload(char *name)
509 struct dcss_segment *seg;
514 mutex_lock(&dcss_lock);
515 seg = segment_by_name (name);
517 PRINT_ERR ("could not find segment %s in segment_unload, "
518 "please report to linux390@de.ibm.com\n",name);
521 if (atomic_dec_return(&seg->ref_count) != 0)
523 release_resource(seg->res);
525 remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
526 list_del(&seg->list);
527 dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
530 mutex_unlock(&dcss_lock);
534 * save segment content permanently
537 segment_save(char *name)
539 struct dcss_segment *seg;
549 mutex_lock(&dcss_lock);
550 seg = segment_by_name (name);
553 PRINT_ERR("could not find segment %s in segment_save, please "
554 "report to linux390@de.ibm.com\n", name);
558 startpfn = seg->start_addr >> PAGE_SHIFT;
559 endpfn = (seg->end) >> PAGE_SHIFT;
560 sprintf(cmd1, "DEFSEG %s", name);
561 for (i=0; i<seg->segcnt; i++) {
562 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
563 seg->range[i].start >> PAGE_SHIFT,
564 seg->range[i].end >> PAGE_SHIFT,
565 segtype_string[seg->range[i].start & 0xff]);
567 sprintf(cmd2, "SAVESEG %s", name);
569 cpcmd(cmd1, NULL, 0, &response);
571 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
575 cpcmd(cmd2, NULL, 0, &response);
577 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
582 mutex_unlock(&dcss_lock);
585 EXPORT_SYMBOL(segment_load);
586 EXPORT_SYMBOL(segment_unload);
587 EXPORT_SYMBOL(segment_save);
588 EXPORT_SYMBOL(segment_type);
589 EXPORT_SYMBOL(segment_modify_shared);