2 * Author(s)......: Carsten Otte <cotte@de.ibm.com>
3 * Rob M van der Heij <rvdheij@nl.ibm.com>
4 * Steven Shultz <shultzss@us.ibm.com>
5 * Bugreports.to..: <Linux390@de.ibm.com>
6 * Copyright IBM Corp. 2002, 2004
9 #define KMSG_COMPONENT "extmem"
10 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12 #include <linux/kernel.h>
13 #include <linux/string.h>
14 #include <linux/spinlock.h>
15 #include <linux/list.h>
16 #include <linux/slab.h>
17 #include <linux/module.h>
18 #include <linux/bootmem.h>
19 #include <linux/ctype.h>
20 #include <linux/ioport.h>
22 #include <asm/pgtable.h>
23 #include <asm/ebcdic.h>
24 #include <asm/errno.h>
25 #include <asm/extmem.h>
26 #include <asm/cpcmd.h>
27 #include <asm/setup.h>
29 #define DCSS_LOADSHR 0x00
30 #define DCSS_LOADNSR 0x04
31 #define DCSS_PURGESEG 0x08
32 #define DCSS_FINDSEG 0x0c
33 #define DCSS_LOADNOLY 0x10
34 #define DCSS_SEGEXT 0x18
35 #define DCSS_LOADSHRX 0x20
36 #define DCSS_LOADNSRX 0x24
37 #define DCSS_FINDSEGX 0x2c
38 #define DCSS_SEGEXTX 0x38
39 #define DCSS_FINDSEGA 0x0c
42 unsigned long start; /* last byte type */
43 unsigned long end; /* last byte reserved */
47 unsigned long segstart;
51 struct qrange range[6];
56 unsigned int start; /* last byte type */
57 unsigned int end; /* last byte reserved */
60 /* output area format for the Diag x'64' old subcode x'18' */
66 struct qrange_old range[6];
81 struct list_head list;
84 unsigned long start_addr;
88 unsigned int vm_segtype;
89 struct qrange range[6];
94 static DEFINE_MUTEX(dcss_lock);
95 static LIST_HEAD(dcss_list);
96 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
98 static int loadshr_scode, loadnsr_scode, findseg_scode;
99 static int segext_scode, purgeseg_scode;
100 static int scode_set;
102 /* set correct Diag x'64' subcodes. */
104 dcss_set_subcodes(void)
107 char *name = kmalloc(8 * sizeof(char), GFP_KERNEL | GFP_DMA);
108 unsigned long rx, ry;
114 rx = (unsigned long) name;
117 strcpy(name, "dummy");
126 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
129 /* Diag x'64' new subcodes are supported, set to new subcodes */
131 loadshr_scode = DCSS_LOADSHRX;
132 loadnsr_scode = DCSS_LOADNSRX;
133 purgeseg_scode = DCSS_PURGESEG;
134 findseg_scode = DCSS_FINDSEGX;
135 segext_scode = DCSS_SEGEXTX;
139 /* Diag x'64' new subcodes are not supported, set to old subcodes */
140 loadshr_scode = DCSS_LOADNOLY;
141 loadnsr_scode = DCSS_LOADNSR;
142 purgeseg_scode = DCSS_PURGESEG;
143 findseg_scode = DCSS_FINDSEG;
144 segext_scode = DCSS_SEGEXT;
149 * Create the 8 bytes, ebcdic VM segment name from
153 dcss_mkname(char *name, char *dcss_name)
157 for (i = 0; i < 8; i++) {
160 dcss_name[i] = toupper(name[i]);
164 ASCEBC(dcss_name, 8);
169 * search all segments in dcss_list, and return the one
170 * namend *name. If not found, return NULL.
172 static struct dcss_segment *
173 segment_by_name (char *name)
177 struct dcss_segment *tmp, *retval = NULL;
179 BUG_ON(!mutex_is_locked(&dcss_lock));
180 dcss_mkname (name, dcss_name);
181 list_for_each (l, &dcss_list) {
182 tmp = list_entry (l, struct dcss_segment, list);
183 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
193 * Perform a function on a dcss segment.
196 dcss_diag(int *func, void *parameter,
197 unsigned long *ret1, unsigned long *ret2)
199 unsigned long rx, ry;
202 if (scode_set == 0) {
203 rc = dcss_set_subcodes();
208 rx = (unsigned long) parameter;
209 ry = (unsigned long) *func;
212 /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
213 if (*func > DCSS_SEGEXT)
218 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
219 /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
227 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
233 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
241 dcss_diag_translate_rc (int vm_rc) {
248 /* do a diag to get info about a segment.
249 * fills start_address, end and vm_segtype fields
252 query_segment_type (struct dcss_segment *seg)
254 unsigned long dummy, vmrc;
259 qin = kmalloc(sizeof(*qin), GFP_KERNEL | GFP_DMA);
260 qout = kmalloc(sizeof(*qout), GFP_KERNEL | GFP_DMA);
261 if ((qin == NULL) || (qout == NULL)) {
266 /* initialize diag input parameters */
267 qin->qopcode = DCSS_FINDSEGA;
268 qin->qoutptr = (unsigned long) qout;
269 qin->qoutlen = sizeof(struct qout64);
270 memcpy (qin->qname, seg->dcss_name, 8);
272 diag_cc = dcss_diag(&segext_scode, qin, &dummy, &vmrc);
279 pr_warning("Querying a DCSS type failed with rc=%ld\n", vmrc);
280 rc = dcss_diag_translate_rc (vmrc);
285 /* Only old format of output area of Diagnose x'64' is supported,
286 copy data for the new format. */
287 if (segext_scode == DCSS_SEGEXT) {
288 struct qout64_old *qout_old;
289 qout_old = kzalloc(sizeof(*qout_old), GFP_KERNEL | GFP_DMA);
290 if (qout_old == NULL) {
294 memcpy(qout_old, qout, sizeof(struct qout64_old));
295 qout->segstart = (unsigned long) qout_old->segstart;
296 qout->segend = (unsigned long) qout_old->segend;
297 qout->segcnt = qout_old->segcnt;
298 qout->segrcnt = qout_old->segrcnt;
300 if (qout->segcnt > 6)
302 for (i = 0; i < qout->segrcnt; i++) {
303 qout->range[i].start =
304 (unsigned long) qout_old->range[i].start;
306 (unsigned long) qout_old->range[i].end;
311 if (qout->segcnt > 6) {
316 if (qout->segcnt == 1) {
317 seg->vm_segtype = qout->range[0].start & 0xff;
319 /* multi-part segment. only one type supported here:
320 - all parts are contiguous
321 - all parts are either EW or EN type
322 - maximum 6 parts allowed */
323 unsigned long start = qout->segstart >> PAGE_SHIFT;
324 for (i=0; i<qout->segcnt; i++) {
325 if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
326 ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
330 if (start != qout->range[i].start >> PAGE_SHIFT) {
334 start = (qout->range[i].end >> PAGE_SHIFT) + 1;
336 seg->vm_segtype = SEG_TYPE_EWEN;
339 /* analyze diag output and update seg */
340 seg->start_addr = qout->segstart;
341 seg->end = qout->segend;
343 memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
344 seg->segcnt = qout->segcnt;
355 * get info about a segment
356 * possible return values:
357 * -ENOSYS : we are not running on VM
358 * -EIO : could not perform query diagnose
359 * -ENOENT : no such segment
360 * -EOPNOTSUPP: multi-part segment cannot be used with linux
361 * -ENOMEM : out of memory
362 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
365 segment_type (char* name)
368 struct dcss_segment seg;
373 dcss_mkname(name, seg.dcss_name);
374 rc = query_segment_type (&seg);
377 return seg.vm_segtype;
381 * check if segment collides with other segments that are currently loaded
382 * returns 1 if this is the case, 0 if no collision was found
385 segment_overlaps_others (struct dcss_segment *seg)
388 struct dcss_segment *tmp;
390 BUG_ON(!mutex_is_locked(&dcss_lock));
391 list_for_each(l, &dcss_list) {
392 tmp = list_entry(l, struct dcss_segment, list);
393 if ((tmp->start_addr >> 20) > (seg->end >> 20))
395 if ((tmp->end >> 20) < (seg->start_addr >> 20))
405 * real segment loading function, called from segment_load
408 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
410 unsigned long start_addr, end_addr, dummy;
411 struct dcss_segment *seg;
414 start_addr = end_addr = 0;
415 seg = kmalloc(sizeof(*seg), GFP_KERNEL | GFP_DMA);
420 dcss_mkname (name, seg->dcss_name);
421 rc = query_segment_type (seg);
425 if (loadshr_scode == DCSS_LOADSHRX) {
426 if (segment_overlaps_others(seg)) {
432 rc = vmem_add_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
437 seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
438 if (seg->res == NULL) {
442 seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
443 seg->res->start = seg->start_addr;
444 seg->res->end = seg->end;
445 memcpy(&seg->res_name, seg->dcss_name, 8);
446 EBCASC(seg->res_name, 8);
447 seg->res_name[8] = '\0';
448 strncat(seg->res_name, " (DCSS)", 7);
449 seg->res->name = seg->res_name;
450 rc = seg->vm_segtype;
451 if (rc == SEG_TYPE_SC ||
452 ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
453 seg->res->flags |= IORESOURCE_READONLY;
454 if (request_resource(&iomem_resource, seg->res)) {
461 diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
462 &start_addr, &end_addr);
464 diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
465 &start_addr, &end_addr);
467 dcss_diag(&purgeseg_scode, seg->dcss_name,
473 pr_warning("Loading DCSS %s failed with rc=%ld\n", name,
475 rc = dcss_diag_translate_rc(end_addr);
476 dcss_diag(&purgeseg_scode, seg->dcss_name,
480 seg->start_addr = start_addr;
482 seg->do_nonshared = do_nonshared;
483 atomic_set(&seg->ref_count, 1);
484 list_add(&seg->list, &dcss_list);
485 *addr = seg->start_addr;
488 pr_info("DCSS %s of range %p to %p and type %s loaded as "
489 "exclusive-writable\n", name, (void*) seg->start_addr,
490 (void*) seg->end, segtype_string[seg->vm_segtype]);
492 pr_info("DCSS %s of range %p to %p and type %s loaded in "
493 "shared access mode\n", name, (void*) seg->start_addr,
494 (void*) seg->end, segtype_string[seg->vm_segtype]);
498 release_resource(seg->res);
501 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
509 * this function loads a DCSS segment
510 * name : name of the DCSS
511 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
512 * 1 indicates that the dcss should be exclusive for this linux image
513 * addr : will be filled with start address of the segment
514 * end : will be filled with end address of the segment
516 * -ENOSYS : we are not running on VM
517 * -EIO : could not perform query or load diagnose
518 * -ENOENT : no such segment
519 * -EOPNOTSUPP: multi-part segment cannot be used with linux
520 * -ENOSPC : segment cannot be used (overlaps with storage)
521 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
522 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
523 * -EPERM : segment is currently loaded with incompatible permissions
524 * -ENOMEM : out of memory
525 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
528 segment_load (char *name, int do_nonshared, unsigned long *addr,
531 struct dcss_segment *seg;
537 mutex_lock(&dcss_lock);
538 seg = segment_by_name (name);
540 rc = __segment_load (name, do_nonshared, addr, end);
542 if (do_nonshared == seg->do_nonshared) {
543 atomic_inc(&seg->ref_count);
544 *addr = seg->start_addr;
546 rc = seg->vm_segtype;
552 mutex_unlock(&dcss_lock);
557 * this function modifies the shared state of a DCSS segment. note that
558 * name : name of the DCSS
559 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
560 * 1 indicates that the dcss should be exclusive for this linux image
562 * -EIO : could not perform load diagnose (segment gone!)
563 * -ENOENT : no such segment (segment gone!)
564 * -EAGAIN : segment is in use by other exploiters, try later
565 * -EINVAL : no segment with the given name is currently loaded - name invalid
566 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
567 * 0 : operation succeeded
570 segment_modify_shared (char *name, int do_nonshared)
572 struct dcss_segment *seg;
573 unsigned long start_addr, end_addr, dummy;
576 start_addr = end_addr = 0;
577 mutex_lock(&dcss_lock);
578 seg = segment_by_name (name);
583 if (do_nonshared == seg->do_nonshared) {
584 pr_info("DCSS %s is already in the requested access "
589 if (atomic_read (&seg->ref_count) != 1) {
590 pr_warning("DCSS %s is in use and cannot be reloaded\n",
595 release_resource(seg->res);
597 seg->res->flags &= ~IORESOURCE_READONLY;
599 if (seg->vm_segtype == SEG_TYPE_SR ||
600 seg->vm_segtype == SEG_TYPE_ER)
601 seg->res->flags |= IORESOURCE_READONLY;
603 if (request_resource(&iomem_resource, seg->res)) {
604 pr_warning("DCSS %s overlaps with used memory resources "
605 "and cannot be reloaded\n", name);
611 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
613 diag_cc = dcss_diag(&loadnsr_scode, seg->dcss_name,
614 &start_addr, &end_addr);
616 diag_cc = dcss_diag(&loadshr_scode, seg->dcss_name,
617 &start_addr, &end_addr);
623 pr_warning("Reloading DCSS %s failed with rc=%ld\n", name,
625 rc = dcss_diag_translate_rc(end_addr);
628 seg->start_addr = start_addr;
630 seg->do_nonshared = do_nonshared;
634 release_resource(seg->res);
637 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
638 list_del(&seg->list);
639 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
642 mutex_unlock(&dcss_lock);
647 * Decrease the use count of a DCSS segment and remove
648 * it from the address space if nobody is using it
652 segment_unload(char *name)
655 struct dcss_segment *seg;
660 mutex_lock(&dcss_lock);
661 seg = segment_by_name (name);
663 pr_err("Unloading unknown DCSS %s failed\n", name);
666 if (atomic_dec_return(&seg->ref_count) != 0)
668 release_resource(seg->res);
670 vmem_remove_mapping(seg->start_addr, seg->end - seg->start_addr + 1);
671 list_del(&seg->list);
672 dcss_diag(&purgeseg_scode, seg->dcss_name, &dummy, &dummy);
675 mutex_unlock(&dcss_lock);
679 * save segment content permanently
682 segment_save(char *name)
684 struct dcss_segment *seg;
692 mutex_lock(&dcss_lock);
693 seg = segment_by_name (name);
696 pr_err("Saving unknown DCSS %s failed\n", name);
700 sprintf(cmd1, "DEFSEG %s", name);
701 for (i=0; i<seg->segcnt; i++) {
702 sprintf(cmd1+strlen(cmd1), " %lX-%lX %s",
703 seg->range[i].start >> PAGE_SHIFT,
704 seg->range[i].end >> PAGE_SHIFT,
705 segtype_string[seg->range[i].start & 0xff]);
707 sprintf(cmd2, "SAVESEG %s", name);
709 cpcmd(cmd1, NULL, 0, &response);
711 pr_err("Saving a DCSS failed with DEFSEG response code "
715 cpcmd(cmd2, NULL, 0, &response);
717 pr_err("Saving a DCSS failed with SAVESEG response code "
722 mutex_unlock(&dcss_lock);
726 * print appropriate error message for segment_load()/segment_type()
729 void segment_warning(int rc, char *seg_name)
733 pr_err("DCSS %s cannot be loaded or queried\n", seg_name);
736 pr_err("DCSS %s cannot be loaded or queried without "
740 pr_err("Loading or querying DCSS %s resulted in a "
741 "hardware error\n", seg_name);
744 pr_err("DCSS %s has multiple page ranges and cannot be "
745 "loaded or queried\n", seg_name);
748 pr_err("DCSS %s overlaps with used storage and cannot "
749 "be loaded\n", seg_name);
752 pr_err("%s needs used memory resources and cannot be "
753 "loaded or queried\n", seg_name);
756 pr_err("DCSS %s is already loaded in a different access "
760 pr_err("There is not enough memory to load or query "
761 "DCSS %s\n", seg_name);
764 pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
765 "and cannot be loaded\n", seg_name, VMEM_MAX_PHYS);
772 EXPORT_SYMBOL(segment_load);
773 EXPORT_SYMBOL(segment_unload);
774 EXPORT_SYMBOL(segment_save);
775 EXPORT_SYMBOL(segment_type);
776 EXPORT_SYMBOL(segment_modify_shared);
777 EXPORT_SYMBOL(segment_warning);