1 // SPDX-License-Identifier: GPL-2.0
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 * Copyright IBM Corp. 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/export.h>
19 #include <linux/bootmem.h>
20 #include <linux/ctype.h>
21 #include <linux/ioport.h>
24 #include <asm/pgtable.h>
25 #include <asm/ebcdic.h>
26 #include <asm/errno.h>
27 #include <asm/extmem.h>
28 #include <asm/cpcmd.h>
29 #include <asm/setup.h>
31 #define DCSS_LOADSHR 0x00
32 #define DCSS_LOADNSR 0x04
33 #define DCSS_PURGESEG 0x08
34 #define DCSS_FINDSEG 0x0c
35 #define DCSS_LOADNOLY 0x10
36 #define DCSS_SEGEXT 0x18
37 #define DCSS_LOADSHRX 0x20
38 #define DCSS_LOADNSRX 0x24
39 #define DCSS_FINDSEGX 0x2c
40 #define DCSS_SEGEXTX 0x38
41 #define DCSS_FINDSEGA 0x0c
44 unsigned long start
; /* last byte type */
45 unsigned long end
; /* last byte reserved */
49 unsigned long segstart
;
53 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];
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
;
99 static int segext_scode
, purgeseg_scode
;
100 static int scode_set
;
102 /* set correct Diag x'64' subcodes. */
104 dcss_set_subcodes(void)
106 char *name
= kmalloc(8 * sizeof(char), GFP_KERNEL
| GFP_DMA
);
107 unsigned long rx
, ry
;
113 rx
= (unsigned long) name
;
116 strcpy(name
, "dummy");
117 diag_stat_inc(DIAG_STAT_X064
);
126 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc", "memory");
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 segext_scode
= DCSS_SEGEXTX
;
137 /* Diag x'64' new subcodes are not supported, set to old subcodes */
138 loadshr_scode
= DCSS_LOADNOLY
;
139 loadnsr_scode
= DCSS_LOADNSR
;
140 purgeseg_scode
= DCSS_PURGESEG
;
141 segext_scode
= DCSS_SEGEXT
;
146 * Create the 8 bytes, ebcdic VM segment name from
150 dcss_mkname(char *name
, char *dcss_name
)
154 for (i
= 0; i
< 8; i
++) {
157 dcss_name
[i
] = toupper(name
[i
]);
161 ASCEBC(dcss_name
, 8);
166 * search all segments in dcss_list, and return the one
167 * namend *name. If not found, return NULL.
169 static struct dcss_segment
*
170 segment_by_name (char *name
)
174 struct dcss_segment
*tmp
, *retval
= NULL
;
176 BUG_ON(!mutex_is_locked(&dcss_lock
));
177 dcss_mkname (name
, dcss_name
);
178 list_for_each (l
, &dcss_list
) {
179 tmp
= list_entry (l
, struct dcss_segment
, list
);
180 if (memcmp(tmp
->dcss_name
, dcss_name
, 8) == 0) {
190 * Perform a function on a dcss segment.
193 dcss_diag(int *func
, void *parameter
,
194 unsigned long *ret1
, unsigned long *ret2
)
196 unsigned long rx
, ry
;
199 if (scode_set
== 0) {
200 rc
= dcss_set_subcodes();
205 rx
= (unsigned long) parameter
;
206 ry
= (unsigned long) *func
;
208 /* 64-bit Diag x'64' new subcode, keep in 64-bit addressing mode */
209 diag_stat_inc(DIAG_STAT_X064
);
210 if (*func
> DCSS_SEGEXT
)
215 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
216 /* 31-bit Diag x'64' old subcode, switch to 31-bit addressing mode */
224 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
231 dcss_diag_translate_rc (int vm_rc
) {
238 /* do a diag to get info about a segment.
239 * fills start_address, end and vm_segtype fields
242 query_segment_type (struct dcss_segment
*seg
)
244 unsigned long dummy
, vmrc
;
249 qin
= kmalloc(sizeof(*qin
), GFP_KERNEL
| GFP_DMA
);
250 qout
= kmalloc(sizeof(*qout
), GFP_KERNEL
| GFP_DMA
);
251 if ((qin
== NULL
) || (qout
== NULL
)) {
256 /* initialize diag input parameters */
257 qin
->qopcode
= DCSS_FINDSEGA
;
258 qin
->qoutptr
= (unsigned long) qout
;
259 qin
->qoutlen
= sizeof(struct qout64
);
260 memcpy (qin
->qname
, seg
->dcss_name
, 8);
262 diag_cc
= dcss_diag(&segext_scode
, qin
, &dummy
, &vmrc
);
269 pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc
);
270 rc
= dcss_diag_translate_rc (vmrc
);
274 /* Only old format of output area of Diagnose x'64' is supported,
275 copy data for the new format. */
276 if (segext_scode
== DCSS_SEGEXT
) {
277 struct qout64_old
*qout_old
;
278 qout_old
= kzalloc(sizeof(*qout_old
), GFP_KERNEL
| GFP_DMA
);
279 if (qout_old
== NULL
) {
283 memcpy(qout_old
, qout
, sizeof(struct qout64_old
));
284 qout
->segstart
= (unsigned long) qout_old
->segstart
;
285 qout
->segend
= (unsigned long) qout_old
->segend
;
286 qout
->segcnt
= qout_old
->segcnt
;
287 qout
->segrcnt
= qout_old
->segrcnt
;
289 if (qout
->segcnt
> 6)
291 for (i
= 0; i
< qout
->segrcnt
; i
++) {
292 qout
->range
[i
].start
=
293 (unsigned long) qout_old
->range
[i
].start
;
295 (unsigned long) qout_old
->range
[i
].end
;
299 if (qout
->segcnt
> 6) {
304 if (qout
->segcnt
== 1) {
305 seg
->vm_segtype
= qout
->range
[0].start
& 0xff;
307 /* multi-part segment. only one type supported here:
308 - all parts are contiguous
309 - all parts are either EW or EN type
310 - maximum 6 parts allowed */
311 unsigned long start
= qout
->segstart
>> PAGE_SHIFT
;
312 for (i
=0; i
<qout
->segcnt
; i
++) {
313 if (((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EW
) &&
314 ((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EN
)) {
318 if (start
!= qout
->range
[i
].start
>> PAGE_SHIFT
) {
322 start
= (qout
->range
[i
].end
>> PAGE_SHIFT
) + 1;
324 seg
->vm_segtype
= SEG_TYPE_EWEN
;
327 /* analyze diag output and update seg */
328 seg
->start_addr
= qout
->segstart
;
329 seg
->end
= qout
->segend
;
331 memcpy (seg
->range
, qout
->range
, 6*sizeof(struct qrange
));
332 seg
->segcnt
= qout
->segcnt
;
343 * get info about a segment
344 * possible return values:
345 * -ENOSYS : we are not running on VM
346 * -EIO : could not perform query diagnose
347 * -ENOENT : no such segment
348 * -EOPNOTSUPP: multi-part segment cannot be used with linux
349 * -ENOMEM : out of memory
350 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
353 segment_type (char* name
)
356 struct dcss_segment seg
;
361 dcss_mkname(name
, seg
.dcss_name
);
362 rc
= query_segment_type (&seg
);
365 return seg
.vm_segtype
;
369 * check if segment collides with other segments that are currently loaded
370 * returns 1 if this is the case, 0 if no collision was found
373 segment_overlaps_others (struct dcss_segment
*seg
)
376 struct dcss_segment
*tmp
;
378 BUG_ON(!mutex_is_locked(&dcss_lock
));
379 list_for_each(l
, &dcss_list
) {
380 tmp
= list_entry(l
, struct dcss_segment
, list
);
381 if ((tmp
->start_addr
>> 20) > (seg
->end
>> 20))
383 if ((tmp
->end
>> 20) < (seg
->start_addr
>> 20))
393 * real segment loading function, called from segment_load
396 __segment_load (char *name
, int do_nonshared
, unsigned long *addr
, unsigned long *end
)
398 unsigned long start_addr
, end_addr
, dummy
;
399 struct dcss_segment
*seg
;
402 start_addr
= end_addr
= 0;
403 seg
= kmalloc(sizeof(*seg
), GFP_KERNEL
| GFP_DMA
);
408 dcss_mkname (name
, seg
->dcss_name
);
409 rc
= query_segment_type (seg
);
413 if (loadshr_scode
== DCSS_LOADSHRX
) {
414 if (segment_overlaps_others(seg
)) {
420 rc
= vmem_add_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
425 seg
->res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
426 if (seg
->res
== NULL
) {
430 seg
->res
->flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
;
431 seg
->res
->start
= seg
->start_addr
;
432 seg
->res
->end
= seg
->end
;
433 memcpy(&seg
->res_name
, seg
->dcss_name
, 8);
434 EBCASC(seg
->res_name
, 8);
435 seg
->res_name
[8] = '\0';
436 strlcat(seg
->res_name
, " (DCSS)", sizeof(seg
->res_name
));
437 seg
->res
->name
= seg
->res_name
;
438 rc
= seg
->vm_segtype
;
439 if (rc
== SEG_TYPE_SC
||
440 ((rc
== SEG_TYPE_SR
|| rc
== SEG_TYPE_ER
) && !do_nonshared
))
441 seg
->res
->flags
|= IORESOURCE_READONLY
;
442 if (request_resource(&iomem_resource
, seg
->res
)) {
449 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
450 &start_addr
, &end_addr
);
452 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
453 &start_addr
, &end_addr
);
455 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
461 pr_warn("Loading DCSS %s failed with rc=%ld\n", name
, end_addr
);
462 rc
= dcss_diag_translate_rc(end_addr
);
463 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
467 seg
->start_addr
= start_addr
;
469 seg
->do_nonshared
= do_nonshared
;
470 atomic_set(&seg
->ref_count
, 1);
471 list_add(&seg
->list
, &dcss_list
);
472 *addr
= seg
->start_addr
;
475 pr_info("DCSS %s of range %p to %p and type %s loaded as "
476 "exclusive-writable\n", name
, (void*) seg
->start_addr
,
477 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
479 pr_info("DCSS %s of range %p to %p and type %s loaded in "
480 "shared access mode\n", name
, (void*) seg
->start_addr
,
481 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
485 release_resource(seg
->res
);
488 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
496 * this function loads a DCSS segment
497 * name : name of the DCSS
498 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
499 * 1 indicates that the dcss should be exclusive for this linux image
500 * addr : will be filled with start address of the segment
501 * end : will be filled with end address of the segment
503 * -ENOSYS : we are not running on VM
504 * -EIO : could not perform query or load diagnose
505 * -ENOENT : no such segment
506 * -EOPNOTSUPP: multi-part segment cannot be used with linux
507 * -ENOSPC : segment cannot be used (overlaps with storage)
508 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
509 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
510 * -EPERM : segment is currently loaded with incompatible permissions
511 * -ENOMEM : out of memory
512 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
515 segment_load (char *name
, int do_nonshared
, unsigned long *addr
,
518 struct dcss_segment
*seg
;
524 mutex_lock(&dcss_lock
);
525 seg
= segment_by_name (name
);
527 rc
= __segment_load (name
, do_nonshared
, addr
, end
);
529 if (do_nonshared
== seg
->do_nonshared
) {
530 atomic_inc(&seg
->ref_count
);
531 *addr
= seg
->start_addr
;
533 rc
= seg
->vm_segtype
;
539 mutex_unlock(&dcss_lock
);
544 * this function modifies the shared state of a DCSS segment. note that
545 * name : name of the DCSS
546 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
547 * 1 indicates that the dcss should be exclusive for this linux image
549 * -EIO : could not perform load diagnose (segment gone!)
550 * -ENOENT : no such segment (segment gone!)
551 * -EAGAIN : segment is in use by other exploiters, try later
552 * -EINVAL : no segment with the given name is currently loaded - name invalid
553 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
554 * 0 : operation succeeded
557 segment_modify_shared (char *name
, int do_nonshared
)
559 struct dcss_segment
*seg
;
560 unsigned long start_addr
, end_addr
, dummy
;
563 start_addr
= end_addr
= 0;
564 mutex_lock(&dcss_lock
);
565 seg
= segment_by_name (name
);
570 if (do_nonshared
== seg
->do_nonshared
) {
571 pr_info("DCSS %s is already in the requested access "
576 if (atomic_read (&seg
->ref_count
) != 1) {
577 pr_warn("DCSS %s is in use and cannot be reloaded\n", name
);
581 release_resource(seg
->res
);
583 seg
->res
->flags
&= ~IORESOURCE_READONLY
;
585 if (seg
->vm_segtype
== SEG_TYPE_SR
||
586 seg
->vm_segtype
== SEG_TYPE_ER
)
587 seg
->res
->flags
|= IORESOURCE_READONLY
;
589 if (request_resource(&iomem_resource
, seg
->res
)) {
590 pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
597 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
599 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
600 &start_addr
, &end_addr
);
602 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
603 &start_addr
, &end_addr
);
609 pr_warn("Reloading DCSS %s failed with rc=%ld\n",
611 rc
= dcss_diag_translate_rc(end_addr
);
614 seg
->start_addr
= start_addr
;
616 seg
->do_nonshared
= do_nonshared
;
620 release_resource(seg
->res
);
623 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
624 list_del(&seg
->list
);
625 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
628 mutex_unlock(&dcss_lock
);
633 * Decrease the use count of a DCSS segment and remove
634 * it from the address space if nobody is using it
638 segment_unload(char *name
)
641 struct dcss_segment
*seg
;
646 mutex_lock(&dcss_lock
);
647 seg
= segment_by_name (name
);
649 pr_err("Unloading unknown DCSS %s failed\n", name
);
652 if (atomic_dec_return(&seg
->ref_count
) != 0)
654 release_resource(seg
->res
);
656 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
657 list_del(&seg
->list
);
658 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
661 mutex_unlock(&dcss_lock
);
665 * save segment content permanently
668 segment_save(char *name
)
670 struct dcss_segment
*seg
;
678 mutex_lock(&dcss_lock
);
679 seg
= segment_by_name (name
);
682 pr_err("Saving unknown DCSS %s failed\n", name
);
686 sprintf(cmd1
, "DEFSEG %s", name
);
687 for (i
=0; i
<seg
->segcnt
; i
++) {
688 sprintf(cmd1
+strlen(cmd1
), " %lX-%lX %s",
689 seg
->range
[i
].start
>> PAGE_SHIFT
,
690 seg
->range
[i
].end
>> PAGE_SHIFT
,
691 segtype_string
[seg
->range
[i
].start
& 0xff]);
693 sprintf(cmd2
, "SAVESEG %s", name
);
695 cpcmd(cmd1
, NULL
, 0, &response
);
697 pr_err("Saving a DCSS failed with DEFSEG response code "
701 cpcmd(cmd2
, NULL
, 0, &response
);
703 pr_err("Saving a DCSS failed with SAVESEG response code "
708 mutex_unlock(&dcss_lock
);
712 * print appropriate error message for segment_load()/segment_type()
715 void segment_warning(int rc
, char *seg_name
)
719 pr_err("DCSS %s cannot be loaded or queried\n", seg_name
);
722 pr_err("DCSS %s cannot be loaded or queried without "
726 pr_err("Loading or querying DCSS %s resulted in a "
727 "hardware error\n", seg_name
);
730 pr_err("DCSS %s has multiple page ranges and cannot be "
731 "loaded or queried\n", seg_name
);
734 pr_err("DCSS %s overlaps with used storage and cannot "
735 "be loaded\n", seg_name
);
738 pr_err("%s needs used memory resources and cannot be "
739 "loaded or queried\n", seg_name
);
742 pr_err("DCSS %s is already loaded in a different access "
746 pr_err("There is not enough memory to load or query "
747 "DCSS %s\n", seg_name
);
750 pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
751 "and cannot be loaded\n", seg_name
, VMEM_MAX_PHYS
);
758 EXPORT_SYMBOL(segment_load
);
759 EXPORT_SYMBOL(segment_unload
);
760 EXPORT_SYMBOL(segment_save
);
761 EXPORT_SYMBOL(segment_type
);
762 EXPORT_SYMBOL(segment_modify_shared
);
763 EXPORT_SYMBOL(segment_warning
);