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 #define KMSG_COMPONENT "extmem"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
14 #include <linux/string.h>
15 #include <linux/spinlock.h>
16 #include <linux/list.h>
17 #include <linux/slab.h>
18 #include <linux/module.h>
19 #include <linux/bootmem.h>
20 #include <linux/ctype.h>
21 #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];
57 unsigned int start
; /* last byte type */
58 unsigned int end
; /* last byte reserved */
61 /* output area format for the Diag x'64' old subcode x'18' */
67 struct qrange_old range
[6];
82 struct list_head list
;
85 unsigned long start_addr
;
89 unsigned int vm_segtype
;
90 struct qrange range
[6];
95 static DEFINE_MUTEX(dcss_lock
);
96 static LIST_HEAD(dcss_list
);
97 static char *segtype_string
[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
99 static int loadshr_scode
, loadnsr_scode
, findseg_scode
;
100 static int segext_scode
, purgeseg_scode
;
101 static int scode_set
;
103 /* set correct Diag x'64' subcodes. */
105 dcss_set_subcodes(void)
108 char *name
= kmalloc(8 * sizeof(char), GFP_KERNEL
| GFP_DMA
);
109 unsigned long rx
, ry
;
115 rx
= (unsigned long) name
;
118 strcpy(name
, "dummy");
127 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
130 /* Diag x'64' new subcodes are supported, set to new subcodes */
132 loadshr_scode
= DCSS_LOADSHRX
;
133 loadnsr_scode
= DCSS_LOADNSRX
;
134 purgeseg_scode
= DCSS_PURGESEG
;
135 findseg_scode
= DCSS_FINDSEGX
;
136 segext_scode
= DCSS_SEGEXTX
;
140 /* Diag x'64' new subcodes are not supported, set to old subcodes */
141 loadshr_scode
= DCSS_LOADNOLY
;
142 loadnsr_scode
= DCSS_LOADNSR
;
143 purgeseg_scode
= DCSS_PURGESEG
;
144 findseg_scode
= DCSS_FINDSEG
;
145 segext_scode
= DCSS_SEGEXT
;
150 * Create the 8 bytes, ebcdic VM segment name from
154 dcss_mkname(char *name
, char *dcss_name
)
158 for (i
= 0; i
< 8; i
++) {
161 dcss_name
[i
] = toupper(name
[i
]);
165 ASCEBC(dcss_name
, 8);
170 * search all segments in dcss_list, and return the one
171 * namend *name. If not found, return NULL.
173 static struct dcss_segment
*
174 segment_by_name (char *name
)
178 struct dcss_segment
*tmp
, *retval
= NULL
;
180 BUG_ON(!mutex_is_locked(&dcss_lock
));
181 dcss_mkname (name
, dcss_name
);
182 list_for_each (l
, &dcss_list
) {
183 tmp
= list_entry (l
, struct dcss_segment
, list
);
184 if (memcmp(tmp
->dcss_name
, dcss_name
, 8) == 0) {
194 * Perform a function on a dcss segment.
197 dcss_diag(int *func
, void *parameter
,
198 unsigned long *ret1
, unsigned long *ret2
)
200 unsigned long rx
, ry
;
203 if (scode_set
== 0) {
204 rc
= dcss_set_subcodes();
209 rx
= (unsigned long) parameter
;
210 ry
= (unsigned long) *func
;
213 /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
214 if (*func
> DCSS_SEGEXT
)
219 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
220 /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
228 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
234 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
242 dcss_diag_translate_rc (int vm_rc
) {
249 /* do a diag to get info about a segment.
250 * fills start_address, end and vm_segtype fields
253 query_segment_type (struct dcss_segment
*seg
)
255 unsigned long dummy
, vmrc
;
260 qin
= kmalloc(sizeof(*qin
), GFP_KERNEL
| GFP_DMA
);
261 qout
= kmalloc(sizeof(*qout
), GFP_KERNEL
| GFP_DMA
);
262 if ((qin
== NULL
) || (qout
== NULL
)) {
267 /* initialize diag input parameters */
268 qin
->qopcode
= DCSS_FINDSEGA
;
269 qin
->qoutptr
= (unsigned long) qout
;
270 qin
->qoutlen
= sizeof(struct qout64
);
271 memcpy (qin
->qname
, seg
->dcss_name
, 8);
273 diag_cc
= dcss_diag(&segext_scode
, qin
, &dummy
, &vmrc
);
280 pr_warning("Querying a DCSS type failed with rc=%ld\n", vmrc
);
281 rc
= dcss_diag_translate_rc (vmrc
);
286 /* Only old format of output area of Diagnose x'64' is supported,
287 copy data for the new format. */
288 if (segext_scode
== DCSS_SEGEXT
) {
289 struct qout64_old
*qout_old
;
290 qout_old
= kzalloc(sizeof(*qout_old
), GFP_KERNEL
| GFP_DMA
);
291 if (qout_old
== NULL
) {
295 memcpy(qout_old
, qout
, sizeof(struct qout64_old
));
296 qout
->segstart
= (unsigned long) qout_old
->segstart
;
297 qout
->segend
= (unsigned long) qout_old
->segend
;
298 qout
->segcnt
= qout_old
->segcnt
;
299 qout
->segrcnt
= qout_old
->segrcnt
;
301 if (qout
->segcnt
> 6)
303 for (i
= 0; i
< qout
->segrcnt
; i
++) {
304 qout
->range
[i
].start
=
305 (unsigned long) qout_old
->range
[i
].start
;
307 (unsigned long) qout_old
->range
[i
].end
;
312 if (qout
->segcnt
> 6) {
317 if (qout
->segcnt
== 1) {
318 seg
->vm_segtype
= qout
->range
[0].start
& 0xff;
320 /* multi-part segment. only one type supported here:
321 - all parts are contiguous
322 - all parts are either EW or EN type
323 - maximum 6 parts allowed */
324 unsigned long start
= qout
->segstart
>> PAGE_SHIFT
;
325 for (i
=0; i
<qout
->segcnt
; i
++) {
326 if (((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EW
) &&
327 ((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EN
)) {
331 if (start
!= qout
->range
[i
].start
>> PAGE_SHIFT
) {
335 start
= (qout
->range
[i
].end
>> PAGE_SHIFT
) + 1;
337 seg
->vm_segtype
= SEG_TYPE_EWEN
;
340 /* analyze diag output and update seg */
341 seg
->start_addr
= qout
->segstart
;
342 seg
->end
= qout
->segend
;
344 memcpy (seg
->range
, qout
->range
, 6*sizeof(struct qrange
));
345 seg
->segcnt
= qout
->segcnt
;
356 * get info about a segment
357 * possible return values:
358 * -ENOSYS : we are not running on VM
359 * -EIO : could not perform query diagnose
360 * -ENOENT : no such segment
361 * -EOPNOTSUPP: multi-part segment cannot be used with linux
362 * -ENOMEM : out of memory
363 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
366 segment_type (char* name
)
369 struct dcss_segment seg
;
374 dcss_mkname(name
, seg
.dcss_name
);
375 rc
= query_segment_type (&seg
);
378 return seg
.vm_segtype
;
382 * check if segment collides with other segments that are currently loaded
383 * returns 1 if this is the case, 0 if no collision was found
386 segment_overlaps_others (struct dcss_segment
*seg
)
389 struct dcss_segment
*tmp
;
391 BUG_ON(!mutex_is_locked(&dcss_lock
));
392 list_for_each(l
, &dcss_list
) {
393 tmp
= list_entry(l
, struct dcss_segment
, list
);
394 if ((tmp
->start_addr
>> 20) > (seg
->end
>> 20))
396 if ((tmp
->end
>> 20) < (seg
->start_addr
>> 20))
406 * real segment loading function, called from segment_load
409 __segment_load (char *name
, int do_nonshared
, unsigned long *addr
, unsigned long *end
)
411 unsigned long start_addr
, end_addr
, dummy
;
412 struct dcss_segment
*seg
;
415 start_addr
= end_addr
= 0;
416 seg
= kmalloc(sizeof(*seg
), GFP_KERNEL
| GFP_DMA
);
421 dcss_mkname (name
, seg
->dcss_name
);
422 rc
= query_segment_type (seg
);
426 if (loadshr_scode
== DCSS_LOADSHRX
) {
427 if (segment_overlaps_others(seg
)) {
433 rc
= vmem_add_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
438 seg
->res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
439 if (seg
->res
== NULL
) {
443 seg
->res
->flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
;
444 seg
->res
->start
= seg
->start_addr
;
445 seg
->res
->end
= seg
->end
;
446 memcpy(&seg
->res_name
, seg
->dcss_name
, 8);
447 EBCASC(seg
->res_name
, 8);
448 seg
->res_name
[8] = '\0';
449 strncat(seg
->res_name
, " (DCSS)", 7);
450 seg
->res
->name
= seg
->res_name
;
451 rc
= seg
->vm_segtype
;
452 if (rc
== SEG_TYPE_SC
||
453 ((rc
== SEG_TYPE_SR
|| rc
== SEG_TYPE_ER
) && !do_nonshared
))
454 seg
->res
->flags
|= IORESOURCE_READONLY
;
455 if (request_resource(&iomem_resource
, seg
->res
)) {
462 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
463 &start_addr
, &end_addr
);
465 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
466 &start_addr
, &end_addr
);
468 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
474 pr_warning("Loading DCSS %s failed with rc=%ld\n", name
,
476 rc
= dcss_diag_translate_rc(end_addr
);
477 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
481 seg
->start_addr
= start_addr
;
483 seg
->do_nonshared
= do_nonshared
;
484 atomic_set(&seg
->ref_count
, 1);
485 list_add(&seg
->list
, &dcss_list
);
486 *addr
= seg
->start_addr
;
489 pr_info("DCSS %s of range %p to %p and type %s loaded as "
490 "exclusive-writable\n", name
, (void*) seg
->start_addr
,
491 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
493 pr_info("DCSS %s of range %p to %p and type %s loaded in "
494 "shared access mode\n", name
, (void*) seg
->start_addr
,
495 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
499 release_resource(seg
->res
);
502 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
510 * this function loads a DCSS segment
511 * name : name of the DCSS
512 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
513 * 1 indicates that the dcss should be exclusive for this linux image
514 * addr : will be filled with start address of the segment
515 * end : will be filled with end address of the segment
517 * -ENOSYS : we are not running on VM
518 * -EIO : could not perform query or load diagnose
519 * -ENOENT : no such segment
520 * -EOPNOTSUPP: multi-part segment cannot be used with linux
521 * -ENOSPC : segment cannot be used (overlaps with storage)
522 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
523 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
524 * -EPERM : segment is currently loaded with incompatible permissions
525 * -ENOMEM : out of memory
526 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
529 segment_load (char *name
, int do_nonshared
, unsigned long *addr
,
532 struct dcss_segment
*seg
;
538 mutex_lock(&dcss_lock
);
539 seg
= segment_by_name (name
);
541 rc
= __segment_load (name
, do_nonshared
, addr
, end
);
543 if (do_nonshared
== seg
->do_nonshared
) {
544 atomic_inc(&seg
->ref_count
);
545 *addr
= seg
->start_addr
;
547 rc
= seg
->vm_segtype
;
553 mutex_unlock(&dcss_lock
);
558 * this function modifies the shared state of a DCSS segment. note that
559 * name : name of the DCSS
560 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
561 * 1 indicates that the dcss should be exclusive for this linux image
563 * -EIO : could not perform load diagnose (segment gone!)
564 * -ENOENT : no such segment (segment gone!)
565 * -EAGAIN : segment is in use by other exploiters, try later
566 * -EINVAL : no segment with the given name is currently loaded - name invalid
567 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
568 * 0 : operation succeeded
571 segment_modify_shared (char *name
, int do_nonshared
)
573 struct dcss_segment
*seg
;
574 unsigned long start_addr
, end_addr
, dummy
;
577 start_addr
= end_addr
= 0;
578 mutex_lock(&dcss_lock
);
579 seg
= segment_by_name (name
);
584 if (do_nonshared
== seg
->do_nonshared
) {
585 pr_info("DCSS %s is already in the requested access "
590 if (atomic_read (&seg
->ref_count
) != 1) {
591 pr_warning("DCSS %s is in use and cannot be reloaded\n",
596 release_resource(seg
->res
);
598 seg
->res
->flags
&= ~IORESOURCE_READONLY
;
600 if (seg
->vm_segtype
== SEG_TYPE_SR
||
601 seg
->vm_segtype
== SEG_TYPE_ER
)
602 seg
->res
->flags
|= IORESOURCE_READONLY
;
604 if (request_resource(&iomem_resource
, seg
->res
)) {
605 pr_warning("DCSS %s overlaps with used memory resources "
606 "and cannot be reloaded\n", name
);
612 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
614 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
615 &start_addr
, &end_addr
);
617 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
618 &start_addr
, &end_addr
);
624 pr_warning("Reloading DCSS %s failed with rc=%ld\n", name
,
626 rc
= dcss_diag_translate_rc(end_addr
);
629 seg
->start_addr
= start_addr
;
631 seg
->do_nonshared
= do_nonshared
;
635 release_resource(seg
->res
);
638 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
639 list_del(&seg
->list
);
640 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
643 mutex_unlock(&dcss_lock
);
648 * Decrease the use count of a DCSS segment and remove
649 * it from the address space if nobody is using it
653 segment_unload(char *name
)
656 struct dcss_segment
*seg
;
661 mutex_lock(&dcss_lock
);
662 seg
= segment_by_name (name
);
664 pr_err("Unloading unknown DCSS %s failed\n", name
);
667 if (atomic_dec_return(&seg
->ref_count
) != 0)
669 release_resource(seg
->res
);
671 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
672 list_del(&seg
->list
);
673 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
676 mutex_unlock(&dcss_lock
);
680 * save segment content permanently
683 segment_save(char *name
)
685 struct dcss_segment
*seg
;
693 mutex_lock(&dcss_lock
);
694 seg
= segment_by_name (name
);
697 pr_err("Saving unknown DCSS %s failed\n", name
);
701 sprintf(cmd1
, "DEFSEG %s", name
);
702 for (i
=0; i
<seg
->segcnt
; i
++) {
703 sprintf(cmd1
+strlen(cmd1
), " %lX-%lX %s",
704 seg
->range
[i
].start
>> PAGE_SHIFT
,
705 seg
->range
[i
].end
>> PAGE_SHIFT
,
706 segtype_string
[seg
->range
[i
].start
& 0xff]);
708 sprintf(cmd2
, "SAVESEG %s", name
);
710 cpcmd(cmd1
, NULL
, 0, &response
);
712 pr_err("Saving a DCSS failed with DEFSEG response code "
716 cpcmd(cmd2
, NULL
, 0, &response
);
718 pr_err("Saving a DCSS failed with SAVESEG response code "
723 mutex_unlock(&dcss_lock
);
727 * print appropriate error message for segment_load()/segment_type()
730 void segment_warning(int rc
, char *seg_name
)
734 pr_err("DCSS %s cannot be loaded or queried\n", seg_name
);
737 pr_err("DCSS %s cannot be loaded or queried without "
741 pr_err("Loading or querying DCSS %s resulted in a "
742 "hardware error\n", seg_name
);
745 pr_err("DCSS %s has multiple page ranges and cannot be "
746 "loaded or queried\n", seg_name
);
749 pr_err("DCSS %s overlaps with used storage and cannot "
750 "be loaded\n", seg_name
);
753 pr_err("%s needs used memory resources and cannot be "
754 "loaded or queried\n", seg_name
);
757 pr_err("DCSS %s is already loaded in a different access "
761 pr_err("There is not enough memory to load or query "
762 "DCSS %s\n", seg_name
);
765 pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
766 "and cannot be loaded\n", seg_name
, VMEM_MAX_PHYS
);
773 EXPORT_SYMBOL(segment_load
);
774 EXPORT_SYMBOL(segment_unload
);
775 EXPORT_SYMBOL(segment_save
);
776 EXPORT_SYMBOL(segment_type
);
777 EXPORT_SYMBOL(segment_modify_shared
);
778 EXPORT_SYMBOL(segment_warning
);