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>
23 #include <asm/pgtable.h>
24 #include <asm/ebcdic.h>
25 #include <asm/errno.h>
26 #include <asm/extmem.h>
27 #include <asm/cpcmd.h>
28 #include <asm/setup.h>
30 #define DCSS_LOADSHR 0x00
31 #define DCSS_LOADNSR 0x04
32 #define DCSS_PURGESEG 0x08
33 #define DCSS_FINDSEG 0x0c
34 #define DCSS_LOADNOLY 0x10
35 #define DCSS_SEGEXT 0x18
36 #define DCSS_LOADSHRX 0x20
37 #define DCSS_LOADNSRX 0x24
38 #define DCSS_FINDSEGX 0x2c
39 #define DCSS_SEGEXTX 0x38
40 #define DCSS_FINDSEGA 0x0c
43 unsigned long start
; /* last byte type */
44 unsigned long end
; /* last byte reserved */
48 unsigned long segstart
;
52 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];
80 struct list_head list
;
83 unsigned long start_addr
;
87 unsigned int vm_segtype
;
88 struct qrange range
[6];
93 static DEFINE_MUTEX(dcss_lock
);
94 static LIST_HEAD(dcss_list
);
95 static char *segtype_string
[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
97 static int loadshr_scode
, loadnsr_scode
;
98 static int segext_scode
, purgeseg_scode
;
101 /* set correct Diag x'64' subcodes. */
103 dcss_set_subcodes(void)
105 char *name
= kmalloc(8 * sizeof(char), GFP_KERNEL
| GFP_DMA
);
106 unsigned long rx
, ry
;
112 rx
= (unsigned long) name
;
115 strcpy(name
, "dummy");
116 diag_stat_inc(DIAG_STAT_X064
);
125 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
128 /* Diag x'64' new subcodes are supported, set to new subcodes */
130 loadshr_scode
= DCSS_LOADSHRX
;
131 loadnsr_scode
= DCSS_LOADNSRX
;
132 purgeseg_scode
= DCSS_PURGESEG
;
133 segext_scode
= DCSS_SEGEXTX
;
136 /* Diag x'64' new subcodes are not supported, set to old subcodes */
137 loadshr_scode
= DCSS_LOADNOLY
;
138 loadnsr_scode
= DCSS_LOADNSR
;
139 purgeseg_scode
= DCSS_PURGESEG
;
140 segext_scode
= DCSS_SEGEXT
;
145 * Create the 8 bytes, ebcdic VM segment name from
149 dcss_mkname(char *name
, char *dcss_name
)
153 for (i
= 0; i
< 8; i
++) {
156 dcss_name
[i
] = toupper(name
[i
]);
160 ASCEBC(dcss_name
, 8);
165 * search all segments in dcss_list, and return the one
166 * namend *name. If not found, return NULL.
168 static struct dcss_segment
*
169 segment_by_name (char *name
)
173 struct dcss_segment
*tmp
, *retval
= NULL
;
175 BUG_ON(!mutex_is_locked(&dcss_lock
));
176 dcss_mkname (name
, dcss_name
);
177 list_for_each (l
, &dcss_list
) {
178 tmp
= list_entry (l
, struct dcss_segment
, list
);
179 if (memcmp(tmp
->dcss_name
, dcss_name
, 8) == 0) {
189 * Perform a function on a dcss segment.
192 dcss_diag(int *func
, void *parameter
,
193 unsigned long *ret1
, unsigned long *ret2
)
195 unsigned long rx
, ry
;
198 if (scode_set
== 0) {
199 rc
= dcss_set_subcodes();
204 rx
= (unsigned long) parameter
;
205 ry
= (unsigned long) *func
;
207 /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
208 diag_stat_inc(DIAG_STAT_X064
);
209 if (*func
> DCSS_SEGEXT
)
214 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
215 /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
223 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
230 dcss_diag_translate_rc (int vm_rc
) {
237 /* do a diag to get info about a segment.
238 * fills start_address, end and vm_segtype fields
241 query_segment_type (struct dcss_segment
*seg
)
243 unsigned long dummy
, vmrc
;
248 qin
= kmalloc(sizeof(*qin
), GFP_KERNEL
| GFP_DMA
);
249 qout
= kmalloc(sizeof(*qout
), GFP_KERNEL
| GFP_DMA
);
250 if ((qin
== NULL
) || (qout
== NULL
)) {
255 /* initialize diag input parameters */
256 qin
->qopcode
= DCSS_FINDSEGA
;
257 qin
->qoutptr
= (unsigned long) qout
;
258 qin
->qoutlen
= sizeof(struct qout64
);
259 memcpy (qin
->qname
, seg
->dcss_name
, 8);
261 diag_cc
= dcss_diag(&segext_scode
, qin
, &dummy
, &vmrc
);
268 pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc
);
269 rc
= dcss_diag_translate_rc (vmrc
);
273 /* Only old format of output area of Diagnose x'64' is supported,
274 copy data for the new format. */
275 if (segext_scode
== DCSS_SEGEXT
) {
276 struct qout64_old
*qout_old
;
277 qout_old
= kzalloc(sizeof(*qout_old
), GFP_KERNEL
| GFP_DMA
);
278 if (qout_old
== NULL
) {
282 memcpy(qout_old
, qout
, sizeof(struct qout64_old
));
283 qout
->segstart
= (unsigned long) qout_old
->segstart
;
284 qout
->segend
= (unsigned long) qout_old
->segend
;
285 qout
->segcnt
= qout_old
->segcnt
;
286 qout
->segrcnt
= qout_old
->segrcnt
;
288 if (qout
->segcnt
> 6)
290 for (i
= 0; i
< qout
->segrcnt
; i
++) {
291 qout
->range
[i
].start
=
292 (unsigned long) qout_old
->range
[i
].start
;
294 (unsigned long) qout_old
->range
[i
].end
;
298 if (qout
->segcnt
> 6) {
303 if (qout
->segcnt
== 1) {
304 seg
->vm_segtype
= qout
->range
[0].start
& 0xff;
306 /* multi-part segment. only one type supported here:
307 - all parts are contiguous
308 - all parts are either EW or EN type
309 - maximum 6 parts allowed */
310 unsigned long start
= qout
->segstart
>> PAGE_SHIFT
;
311 for (i
=0; i
<qout
->segcnt
; i
++) {
312 if (((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EW
) &&
313 ((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EN
)) {
317 if (start
!= qout
->range
[i
].start
>> PAGE_SHIFT
) {
321 start
= (qout
->range
[i
].end
>> PAGE_SHIFT
) + 1;
323 seg
->vm_segtype
= SEG_TYPE_EWEN
;
326 /* analyze diag output and update seg */
327 seg
->start_addr
= qout
->segstart
;
328 seg
->end
= qout
->segend
;
330 memcpy (seg
->range
, qout
->range
, 6*sizeof(struct qrange
));
331 seg
->segcnt
= qout
->segcnt
;
342 * get info about a segment
343 * possible return values:
344 * -ENOSYS : we are not running on VM
345 * -EIO : could not perform query diagnose
346 * -ENOENT : no such segment
347 * -EOPNOTSUPP: multi-part segment cannot be used with linux
348 * -ENOMEM : out of memory
349 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
352 segment_type (char* name
)
355 struct dcss_segment seg
;
360 dcss_mkname(name
, seg
.dcss_name
);
361 rc
= query_segment_type (&seg
);
364 return seg
.vm_segtype
;
368 * check if segment collides with other segments that are currently loaded
369 * returns 1 if this is the case, 0 if no collision was found
372 segment_overlaps_others (struct dcss_segment
*seg
)
375 struct dcss_segment
*tmp
;
377 BUG_ON(!mutex_is_locked(&dcss_lock
));
378 list_for_each(l
, &dcss_list
) {
379 tmp
= list_entry(l
, struct dcss_segment
, list
);
380 if ((tmp
->start_addr
>> 20) > (seg
->end
>> 20))
382 if ((tmp
->end
>> 20) < (seg
->start_addr
>> 20))
392 * real segment loading function, called from segment_load
395 __segment_load (char *name
, int do_nonshared
, unsigned long *addr
, unsigned long *end
)
397 unsigned long start_addr
, end_addr
, dummy
;
398 struct dcss_segment
*seg
;
401 start_addr
= end_addr
= 0;
402 seg
= kmalloc(sizeof(*seg
), GFP_KERNEL
| GFP_DMA
);
407 dcss_mkname (name
, seg
->dcss_name
);
408 rc
= query_segment_type (seg
);
412 if (loadshr_scode
== DCSS_LOADSHRX
) {
413 if (segment_overlaps_others(seg
)) {
419 rc
= vmem_add_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
424 seg
->res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
425 if (seg
->res
== NULL
) {
429 seg
->res
->flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
;
430 seg
->res
->start
= seg
->start_addr
;
431 seg
->res
->end
= seg
->end
;
432 memcpy(&seg
->res_name
, seg
->dcss_name
, 8);
433 EBCASC(seg
->res_name
, 8);
434 seg
->res_name
[8] = '\0';
435 strncat(seg
->res_name
, " (DCSS)", 7);
436 seg
->res
->name
= seg
->res_name
;
437 rc
= seg
->vm_segtype
;
438 if (rc
== SEG_TYPE_SC
||
439 ((rc
== SEG_TYPE_SR
|| rc
== SEG_TYPE_ER
) && !do_nonshared
))
440 seg
->res
->flags
|= IORESOURCE_READONLY
;
441 if (request_resource(&iomem_resource
, seg
->res
)) {
448 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
449 &start_addr
, &end_addr
);
451 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
452 &start_addr
, &end_addr
);
454 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
460 pr_warn("Loading DCSS %s failed with rc=%ld\n", name
, end_addr
);
461 rc
= dcss_diag_translate_rc(end_addr
);
462 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
466 seg
->start_addr
= start_addr
;
468 seg
->do_nonshared
= do_nonshared
;
469 atomic_set(&seg
->ref_count
, 1);
470 list_add(&seg
->list
, &dcss_list
);
471 *addr
= seg
->start_addr
;
474 pr_info("DCSS %s of range %p to %p and type %s loaded as "
475 "exclusive-writable\n", name
, (void*) seg
->start_addr
,
476 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
478 pr_info("DCSS %s of range %p to %p and type %s loaded in "
479 "shared access mode\n", name
, (void*) seg
->start_addr
,
480 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
484 release_resource(seg
->res
);
487 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
495 * this function loads a DCSS segment
496 * name : name of the DCSS
497 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
498 * 1 indicates that the dcss should be exclusive for this linux image
499 * addr : will be filled with start address of the segment
500 * end : will be filled with end address of the segment
502 * -ENOSYS : we are not running on VM
503 * -EIO : could not perform query or load diagnose
504 * -ENOENT : no such segment
505 * -EOPNOTSUPP: multi-part segment cannot be used with linux
506 * -ENOSPC : segment cannot be used (overlaps with storage)
507 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
508 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
509 * -EPERM : segment is currently loaded with incompatible permissions
510 * -ENOMEM : out of memory
511 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
514 segment_load (char *name
, int do_nonshared
, unsigned long *addr
,
517 struct dcss_segment
*seg
;
523 mutex_lock(&dcss_lock
);
524 seg
= segment_by_name (name
);
526 rc
= __segment_load (name
, do_nonshared
, addr
, end
);
528 if (do_nonshared
== seg
->do_nonshared
) {
529 atomic_inc(&seg
->ref_count
);
530 *addr
= seg
->start_addr
;
532 rc
= seg
->vm_segtype
;
538 mutex_unlock(&dcss_lock
);
543 * this function modifies the shared state of a DCSS segment. note that
544 * name : name of the DCSS
545 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
546 * 1 indicates that the dcss should be exclusive for this linux image
548 * -EIO : could not perform load diagnose (segment gone!)
549 * -ENOENT : no such segment (segment gone!)
550 * -EAGAIN : segment is in use by other exploiters, try later
551 * -EINVAL : no segment with the given name is currently loaded - name invalid
552 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
553 * 0 : operation succeeded
556 segment_modify_shared (char *name
, int do_nonshared
)
558 struct dcss_segment
*seg
;
559 unsigned long start_addr
, end_addr
, dummy
;
562 start_addr
= end_addr
= 0;
563 mutex_lock(&dcss_lock
);
564 seg
= segment_by_name (name
);
569 if (do_nonshared
== seg
->do_nonshared
) {
570 pr_info("DCSS %s is already in the requested access "
575 if (atomic_read (&seg
->ref_count
) != 1) {
576 pr_warn("DCSS %s is in use and cannot be reloaded\n", name
);
580 release_resource(seg
->res
);
582 seg
->res
->flags
&= ~IORESOURCE_READONLY
;
584 if (seg
->vm_segtype
== SEG_TYPE_SR
||
585 seg
->vm_segtype
== SEG_TYPE_ER
)
586 seg
->res
->flags
|= IORESOURCE_READONLY
;
588 if (request_resource(&iomem_resource
, seg
->res
)) {
589 pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
596 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
598 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
599 &start_addr
, &end_addr
);
601 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
602 &start_addr
, &end_addr
);
608 pr_warn("Reloading DCSS %s failed with rc=%ld\n",
610 rc
= dcss_diag_translate_rc(end_addr
);
613 seg
->start_addr
= start_addr
;
615 seg
->do_nonshared
= do_nonshared
;
619 release_resource(seg
->res
);
622 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
623 list_del(&seg
->list
);
624 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
627 mutex_unlock(&dcss_lock
);
632 * Decrease the use count of a DCSS segment and remove
633 * it from the address space if nobody is using it
637 segment_unload(char *name
)
640 struct dcss_segment
*seg
;
645 mutex_lock(&dcss_lock
);
646 seg
= segment_by_name (name
);
648 pr_err("Unloading unknown DCSS %s failed\n", name
);
651 if (atomic_dec_return(&seg
->ref_count
) != 0)
653 release_resource(seg
->res
);
655 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
656 list_del(&seg
->list
);
657 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
660 mutex_unlock(&dcss_lock
);
664 * save segment content permanently
667 segment_save(char *name
)
669 struct dcss_segment
*seg
;
677 mutex_lock(&dcss_lock
);
678 seg
= segment_by_name (name
);
681 pr_err("Saving unknown DCSS %s failed\n", name
);
685 sprintf(cmd1
, "DEFSEG %s", name
);
686 for (i
=0; i
<seg
->segcnt
; i
++) {
687 sprintf(cmd1
+strlen(cmd1
), " %lX-%lX %s",
688 seg
->range
[i
].start
>> PAGE_SHIFT
,
689 seg
->range
[i
].end
>> PAGE_SHIFT
,
690 segtype_string
[seg
->range
[i
].start
& 0xff]);
692 sprintf(cmd2
, "SAVESEG %s", name
);
694 cpcmd(cmd1
, NULL
, 0, &response
);
696 pr_err("Saving a DCSS failed with DEFSEG response code "
700 cpcmd(cmd2
, NULL
, 0, &response
);
702 pr_err("Saving a DCSS failed with SAVESEG response code "
707 mutex_unlock(&dcss_lock
);
711 * print appropriate error message for segment_load()/segment_type()
714 void segment_warning(int rc
, char *seg_name
)
718 pr_err("DCSS %s cannot be loaded or queried\n", seg_name
);
721 pr_err("DCSS %s cannot be loaded or queried without "
725 pr_err("Loading or querying DCSS %s resulted in a "
726 "hardware error\n", seg_name
);
729 pr_err("DCSS %s has multiple page ranges and cannot be "
730 "loaded or queried\n", seg_name
);
733 pr_err("DCSS %s overlaps with used storage and cannot "
734 "be loaded\n", seg_name
);
737 pr_err("%s needs used memory resources and cannot be "
738 "loaded or queried\n", seg_name
);
741 pr_err("DCSS %s is already loaded in a different access "
745 pr_err("There is not enough memory to load or query "
746 "DCSS %s\n", seg_name
);
749 pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
750 "and cannot be loaded\n", seg_name
, VMEM_MAX_PHYS
);
757 EXPORT_SYMBOL(segment_load
);
758 EXPORT_SYMBOL(segment_unload
);
759 EXPORT_SYMBOL(segment_save
);
760 EXPORT_SYMBOL(segment_type
);
761 EXPORT_SYMBOL(segment_modify_shared
);
762 EXPORT_SYMBOL(segment_warning
);