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_LOADSHRX 0x20
47 #define DCSS_LOADNSRX 0x24
48 #define DCSS_FINDSEGX 0x2c
49 #define DCSS_SEGEXTX 0x38
50 #define DCSS_FINDSEGA 0x0c
53 unsigned long start; /* last byte type */
54 unsigned long end; /* last byte reserved */
58 unsigned long segstart;
62 struct qrange range[6];
67 unsigned int start; /* last byte type */
68 unsigned int end; /* last byte reserved */
71 /* output area format for the Diag x'64' old subcode x'18' */
77 struct qrange_old range[6];
92 struct list_head list;
95 unsigned long start_addr;
99 unsigned int vm_segtype;
100 struct qrange range[6];
102 struct resource *res;
105 static DEFINE_MUTEX(dcss_lock);
106 static LIST_HEAD(dcss_list);
107 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
109 static int loadshr_scode, loadnsr_scode, findseg_scode;
110 static int segext_scode, purgeseg_scode;
111 static int scode_set;
113 /* set correct Diag x'64' subcodes. */
115 dcss_set_subcodes(void)
118 char *name = kmalloc(8 * sizeof(char), GFP_DMA);
119 unsigned long rx, ry;
125 rx = (unsigned long) name;
128 strcpy(name, "dummy");
137 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
140 /* Diag x'64' new subcodes are supported, set to new subcodes */
142 loadshr_scode = DCSS_LOADSHRX;
143 loadnsr_scode = DCSS_LOADNSRX;
144 purgeseg_scode = DCSS_PURGESEG;
145 findseg_scode = DCSS_FINDSEGX;
146 segext_scode = DCSS_SEGEXTX;
150 /* Diag x'64' new subcodes are not supported, set to old subcodes */
151 loadshr_scode = DCSS_LOADNOLY;
152 loadnsr_scode = DCSS_LOADNSR;
153 purgeseg_scode = DCSS_PURGESEG;
154 findseg_scode = DCSS_FINDSEG;
155 segext_scode = DCSS_SEGEXT;
160 * Create the 8 bytes, ebcdic VM segment name from
164 dcss_mkname(char *name, char *dcss_name)
168 for (i = 0; i < 8; i++) {
171 dcss_name[i] = toupper(name[i]);
175 ASCEBC(dcss_name, 8);
180 * search all segments in dcss_list, and return the one
181 * namend *name. If not found, return NULL.
183 static struct dcss_segment *
184 segment_by_name (char *name)
188 struct dcss_segment *tmp, *retval = NULL;
190 BUG_ON(!mutex_is_locked(&dcss_lock));
191 dcss_mkname (name, dcss_name);
192 list_for_each (l, &dcss_list) {
193 tmp = list_entry (l, struct dcss_segment, list);
194 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
204 * Perform a function on a dcss segment.
207 dcss_diag(int *func, void *parameter,
208 unsigned long *ret1, unsigned long *ret2)
210 unsigned long rx, ry;
213 if (scode_set == 0) {
214 rc = dcss_set_subcodes();
219 rx = (unsigned long) parameter;
220 ry = (unsigned long) *func;
223 /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
224 if (*func > DCSS_SEGEXT)
229 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
230 /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
238 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
244 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
252 dcss_diag_translate_rc (int vm_rc) {
259 /* do a diag to get info about a segment.
260 * fills start_address, end and vm_segtype fields
263 query_segment_type (struct dcss_segment *seg)
265 struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
266 struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
269 unsigned long dummy, vmrc;
271 if ((qin == NULL) || (qout == NULL)) {
276 /* initialize diag input parameters */
277 qin->qopcode = DCSS_FINDSEGA;
278 qin->qoutptr = (unsigned long) qout;
279 qin->qoutlen = sizeof(struct qout64);
280 memcpy (qin->qname, seg->dcss_name, 8);
282 diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
289 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
290 rc = dcss_diag_translate_rc (vmrc);
295 /* Only old format of output area of Diagnose x'64' is supported,
296 copy data for the new format. */
297 if (segext_scode == DCSS_SEGEXT) {
298 struct qout64_old *qout_old;
299 qout_old = kzalloc(sizeof(struct qout64_old), GFP_DMA);
300 if (qout_old == NULL) {
304 memcpy(qout_old, qout, sizeof(struct qout64_old));
305 qout->segstart = (unsigned long) qout_old->segstart;
306 qout->segend = (unsigned long) qout_old->segend;
307 qout->segcnt = qout_old->segcnt;
308 qout->segrcnt = qout_old->segrcnt;
310 if (qout->segcnt > 6)
312 for (i = 0; i < qout->segrcnt; i++) {
313 qout->range[i].start =
314 (unsigned long) qout_old->range[i].start;
316 (unsigned long) qout_old->range[i].end;
321 if (qout->segcnt > 6) {
326 if (qout->segcnt == 1) {
327 seg->vm_segtype = qout->range[0].start & 0xff;
329 /* multi-part segment. only one type supported here:
330 - all parts are contiguous
331 - all parts are either EW or EN type
332 - maximum 6 parts allowed */
333 unsigned long start = qout->segstart >> PAGE_SHIFT;
334 for (i=0; i<qout->segcnt; i++) {
335 if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
336 ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
340 if (start != qout->range[i].start >> PAGE_SHIFT) {
344 start = (qout->range[i].end >> PAGE_SHIFT) + 1;
346 seg->vm_segtype = SEG_TYPE_EWEN;
349 /* analyze diag output and update seg */
350 seg->start_addr = qout->segstart;
351 seg->end = qout->segend;
353 memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
354 seg->segcnt = qout->segcnt;
365 * get info about a segment
366 * possible return values:
367 * -ENOSYS : we are not running on VM
368 * -EIO : could not perform query diagnose
369 * -ENOENT : no such segment
370 * -ENOTSUPP: multi-part segment cannot be used with linux
371 * -ENOSPC : segment cannot be used (overlaps with storage)
372 * -ENOMEM : out of memory
373 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
376 segment_type (char* name)
379 struct dcss_segment seg;
384 dcss_mkname(name, seg.dcss_name);
385 rc = query_segment_type (&seg);
388 return seg.vm_segtype;
392 * check if segment collides with other segments that are currently loaded
393 * returns 1 if this is the case, 0 if no collision was found
396 segment_overlaps_others (struct dcss_segment *seg)
399 struct dcss_segment *tmp;
401 BUG_ON(!mutex_is_locked(&dcss_lock));
402 list_for_each(l, &dcss_list) {
403 tmp = list_entry(l, struct dcss_segment, list);
404 if ((tmp->start_addr >> 20) > (seg->end >> 20))
406 if ((tmp->end >> 20) < (seg->start_addr >> 20))
416 * real segment loading function, called from segment_load
419 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
421 struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
424 unsigned long start_addr, end_addr, dummy;
430 dcss_mkname (name, seg->dcss_name);
431 rc = query_segment_type (seg);
435 if (loadshr_scode == DCSS_LOADSHRX) {
436 if (segment_overlaps_others(seg)) {
442 rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
447 seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
448 if (seg->res == NULL) {
452 seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
453 seg->res->start = seg->start_addr;
454 seg->res->end = seg->end;
455 memcpy(&seg->res_name, seg->dcss_name, 8);
456 EBCASC(seg->res_name, 8);
457 seg->res_name[8] = '\0';
458 strncat(seg->res_name, " (DCSS)", 7);
459 seg->res->name = seg->res_name;
460 rc = seg->vm_segtype;
461 if (rc == SEG_TYPE_SC ||
462 ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
463 seg->res->flags |= IORESOURCE_READONLY;
464 if (request_resource(&iomem_resource, seg->res)) {
471 diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
472 &start_addr, &end_addr);
474 diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
475 &start_addr, &end_addr);
477 dcss_diag(&purgeseg_scode, seg->dcss_name,
483 PRINT_WARN ("segment_load: could not load segment %s - "
484 "diag returned error (%ld)\n",
486 rc = dcss_diag_translate_rc(end_addr);
487 dcss_diag(&purgeseg_scode, seg->dcss_name,
491 seg->start_addr = start_addr;
493 seg->do_nonshared = do_nonshared;
494 atomic_set(&seg->ref_count, 1);
495 list_add(&seg->list, &dcss_list);
496 *addr = seg->start_addr;
499 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
500 "type %s in non-shared mode\n", name,
501 (void*)seg->start_addr, (void*)seg->end,
502 segtype_string[seg->vm_segtype]);
504 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
505 "type %s in shared mode\n", name,
506 (void*)seg->start_addr, (void*)seg->end,
507 segtype_string[seg->vm_segtype]);
511 release_resource(seg->res);
514 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
522 * this function loads a DCSS segment
523 * name : name of the DCSS
524 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
525 * 1 indicates that the dcss should be exclusive for this linux image
526 * addr : will be filled with start address of the segment
527 * end : will be filled with end address of the segment
529 * -ENOSYS : we are not running on VM
530 * -EIO : could not perform query or load diagnose
531 * -ENOENT : no such segment
532 * -ENOTSUPP: multi-part segment cannot be used with linux
533 * -ENOSPC : segment cannot be used (overlaps with storage)
534 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
535 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
536 * -EPERM : segment is currently loaded with incompatible permissions
537 * -ENOMEM : out of memory
538 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
541 segment_load (char *name, int do_nonshared, unsigned long *addr,
544 struct dcss_segment *seg;
550 mutex_lock(&dcss_lock);
551 seg = segment_by_name (name);
553 rc = __segment_load (name, do_nonshared, addr, end);
555 if (do_nonshared == seg->do_nonshared) {
556 atomic_inc(&seg->ref_count);
557 *addr = seg->start_addr;
559 rc = seg->vm_segtype;
565 mutex_unlock(&dcss_lock);
570 * this function modifies the shared state of a DCSS segment. note that
571 * name : name of the DCSS
572 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
573 * 1 indicates that the dcss should be exclusive for this linux image
575 * -EIO : could not perform load diagnose (segment gone!)
576 * -ENOENT : no such segment (segment gone!)
577 * -EAGAIN : segment is in use by other exploiters, try later
578 * -EINVAL : no segment with the given name is currently loaded - name invalid
579 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
580 * 0 : operation succeeded
583 segment_modify_shared (char *name, int do_nonshared)
585 struct dcss_segment *seg;
586 unsigned long start_addr, end_addr, dummy;
589 mutex_lock(&dcss_lock);
590 seg = segment_by_name (name);
595 if (do_nonshared == seg->do_nonshared) {
596 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
597 " - already in requested mode\n",name);
601 if (atomic_read (&seg->ref_count) != 1) {
602 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
603 "segment is in use by other driver(s)\n",name);
607 release_resource(seg->res);
609 seg->res->flags &= ~IORESOURCE_READONLY;
611 if (seg->vm_segtype == SEG_TYPE_SR ||
612 seg->vm_segtype == SEG_TYPE_ER)
613 seg->res->flags |= IORESOURCE_READONLY;
615 if (request_resource(&iomem_resource, seg->res)) {
616 PRINT_WARN("segment_modify_shared: could not reload segment %s"
617 " - overlapping resources\n", name);
623 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
625 diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
626 &start_addr, &end_addr);
628 diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
629 &start_addr, &end_addr);
635 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
636 " - diag returned error (%ld)\n",
638 rc = dcss_diag_translate_rc(end_addr);
641 seg->start_addr = start_addr;
643 seg->do_nonshared = do_nonshared;
647 release_resource(seg->res);
650 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
651 list_del(&seg->list);
652 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
655 mutex_unlock(&dcss_lock);
660 * Decrease the use count of a DCSS segment and remove
661 * it from the address space if nobody is using it
665 segment_unload(char *name)
668 struct dcss_segment *seg;
673 mutex_lock(&dcss_lock);
674 seg = segment_by_name (name);
676 PRINT_ERR ("could not find segment %s in segment_unload, "
677 "please report to linux390@de.ibm.com\n",name);
680 if (atomic_dec_return(&seg->ref_count) != 0)
682 release_resource(seg->res);
684 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
685 list_del(&seg->list);
686 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
689 mutex_unlock(&dcss_lock);
693 * save segment content permanently
696 segment_save(char *name)
698 struct dcss_segment *seg;
708 mutex_lock(&dcss_lock);
709 seg = segment_by_name (name);
712 PRINT_ERR("could not find segment %s in segment_save, please "
713 "report to linux390@de.ibm.com\n", name);
717 startpfn = seg->start_addr >> PAGE_SHIFT;
718 endpfn = (seg->end) >> PAGE_SHIFT;
719 sprintf(cmd1, "DEFSEG %s", name);
720 for (i=0; i<seg->segcnt; i++) {
721 sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
722 seg->range[i].start >> PAGE_SHIFT,
723 seg->range[i].end >> PAGE_SHIFT,
724 segtype_string[seg->range[i].start & 0xff]);
726 sprintf(cmd2, "SAVESEG %s", name);
728 cpcmd(cmd1, NULL, 0, &response);
730 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
734 cpcmd(cmd2, NULL, 0, &response);
736 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
741 mutex_unlock(&dcss_lock);
745 * print appropriate error message for segment_load()/segment_type()
748 void segment_warning(int rc, char *seg_name)
752 PRINT_WARN("cannot load/query segment %s, "
753 "does not exist\n", seg_name);
756 PRINT_WARN("cannot load/query segment %s, "
757 "not running on VM\n", seg_name);
760 PRINT_WARN("cannot load/query segment %s, "
761 "hardware error\n", seg_name);
764 PRINT_WARN("cannot load/query segment %s, "
765 "is a multi-part segment\n", seg_name);
768 PRINT_WARN("cannot load/query segment %s, "
769 "overlaps with storage\n", seg_name);
772 PRINT_WARN("cannot load/query segment %s, "
773 "overlaps with already loaded dcss\n", seg_name);
776 PRINT_WARN("cannot load/query segment %s, "
777 "already loaded in incompatible mode\n", seg_name);
780 PRINT_WARN("cannot load/query segment %s, "
781 "out of memory\n", seg_name);
784 PRINT_WARN("cannot load/query segment %s, "
785 "exceeds kernel mapping range\n", seg_name);
788 PRINT_WARN("cannot load/query segment %s, "
789 "return value %i\n", seg_name, rc);
794 EXPORT_SYMBOL(segment_load);
795 EXPORT_SYMBOL(segment_unload);
796 EXPORT_SYMBOL(segment_save);
797 EXPORT_SYMBOL(segment_type);
798 EXPORT_SYMBOL(segment_modify_shared);
799 EXPORT_SYMBOL(segment_warning);