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/memblock.h>
20 #include <linux/ctype.h>
21 #include <linux/ioport.h>
22 #include <linux/refcount.h>
25 #include <asm/pgtable.h>
26 #include <asm/ebcdic.h>
27 #include <asm/errno.h>
28 #include <asm/extmem.h>
29 #include <asm/cpcmd.h>
30 #include <asm/setup.h>
32 #define DCSS_PURGESEG 0x08
33 #define DCSS_LOADSHRX 0x20
34 #define DCSS_LOADNSRX 0x24
35 #define DCSS_FINDSEGX 0x2c
36 #define DCSS_SEGEXTX 0x38
37 #define DCSS_FINDSEGA 0x0c
40 unsigned long start
; /* last byte type */
41 unsigned long end
; /* last byte reserved */
45 unsigned long segstart
;
49 struct qrange range
[6];
63 struct list_head list
;
66 unsigned long start_addr
;
70 unsigned int vm_segtype
;
71 struct qrange range
[6];
76 static DEFINE_MUTEX(dcss_lock
);
77 static LIST_HEAD(dcss_list
);
78 static char *segtype_string
[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
80 static int loadshr_scode
= DCSS_LOADSHRX
;
81 static int loadnsr_scode
= DCSS_LOADNSRX
;
82 static int purgeseg_scode
= DCSS_PURGESEG
;
83 static int segext_scode
= DCSS_SEGEXTX
;
86 * Create the 8 bytes, ebcdic VM segment name from
90 dcss_mkname(char *name
, char *dcss_name
)
94 for (i
= 0; i
< 8; i
++) {
97 dcss_name
[i
] = toupper(name
[i
]);
101 ASCEBC(dcss_name
, 8);
106 * search all segments in dcss_list, and return the one
107 * namend *name. If not found, return NULL.
109 static struct dcss_segment
*
110 segment_by_name (char *name
)
114 struct dcss_segment
*tmp
, *retval
= NULL
;
116 BUG_ON(!mutex_is_locked(&dcss_lock
));
117 dcss_mkname (name
, dcss_name
);
118 list_for_each (l
, &dcss_list
) {
119 tmp
= list_entry (l
, struct dcss_segment
, list
);
120 if (memcmp(tmp
->dcss_name
, dcss_name
, 8) == 0) {
130 * Perform a function on a dcss segment.
133 dcss_diag(int *func
, void *parameter
,
134 unsigned long *ret1
, unsigned long *ret2
)
136 unsigned long rx
, ry
;
139 rx
= (unsigned long) parameter
;
140 ry
= (unsigned long) *func
;
142 diag_stat_inc(DIAG_STAT_X064
);
147 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc");
154 dcss_diag_translate_rc (int vm_rc
) {
161 /* do a diag to get info about a segment.
162 * fills start_address, end and vm_segtype fields
165 query_segment_type (struct dcss_segment
*seg
)
167 unsigned long dummy
, vmrc
;
172 qin
= kmalloc(sizeof(*qin
), GFP_KERNEL
| GFP_DMA
);
173 qout
= kmalloc(sizeof(*qout
), GFP_KERNEL
| GFP_DMA
);
174 if ((qin
== NULL
) || (qout
== NULL
)) {
179 /* initialize diag input parameters */
180 qin
->qopcode
= DCSS_FINDSEGA
;
181 qin
->qoutptr
= (unsigned long) qout
;
182 qin
->qoutlen
= sizeof(struct qout64
);
183 memcpy (qin
->qname
, seg
->dcss_name
, 8);
185 diag_cc
= dcss_diag(&segext_scode
, qin
, &dummy
, &vmrc
);
192 pr_warn("Querying a DCSS type failed with rc=%ld\n", vmrc
);
193 rc
= dcss_diag_translate_rc (vmrc
);
197 if (qout
->segcnt
> 6) {
202 if (qout
->segcnt
== 1) {
203 seg
->vm_segtype
= qout
->range
[0].start
& 0xff;
205 /* multi-part segment. only one type supported here:
206 - all parts are contiguous
207 - all parts are either EW or EN type
208 - maximum 6 parts allowed */
209 unsigned long start
= qout
->segstart
>> PAGE_SHIFT
;
210 for (i
=0; i
<qout
->segcnt
; i
++) {
211 if (((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EW
) &&
212 ((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EN
)) {
216 if (start
!= qout
->range
[i
].start
>> PAGE_SHIFT
) {
220 start
= (qout
->range
[i
].end
>> PAGE_SHIFT
) + 1;
222 seg
->vm_segtype
= SEG_TYPE_EWEN
;
225 /* analyze diag output and update seg */
226 seg
->start_addr
= qout
->segstart
;
227 seg
->end
= qout
->segend
;
229 memcpy (seg
->range
, qout
->range
, 6*sizeof(struct qrange
));
230 seg
->segcnt
= qout
->segcnt
;
241 * get info about a segment
242 * possible return values:
243 * -ENOSYS : we are not running on VM
244 * -EIO : could not perform query diagnose
245 * -ENOENT : no such segment
246 * -EOPNOTSUPP: multi-part segment cannot be used with linux
247 * -ENOMEM : out of memory
248 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
251 segment_type (char* name
)
254 struct dcss_segment seg
;
259 dcss_mkname(name
, seg
.dcss_name
);
260 rc
= query_segment_type (&seg
);
263 return seg
.vm_segtype
;
267 * check if segment collides with other segments that are currently loaded
268 * returns 1 if this is the case, 0 if no collision was found
271 segment_overlaps_others (struct dcss_segment
*seg
)
274 struct dcss_segment
*tmp
;
276 BUG_ON(!mutex_is_locked(&dcss_lock
));
277 list_for_each(l
, &dcss_list
) {
278 tmp
= list_entry(l
, struct dcss_segment
, list
);
279 if ((tmp
->start_addr
>> 20) > (seg
->end
>> 20))
281 if ((tmp
->end
>> 20) < (seg
->start_addr
>> 20))
291 * real segment loading function, called from segment_load
294 __segment_load (char *name
, int do_nonshared
, unsigned long *addr
, unsigned long *end
)
296 unsigned long start_addr
, end_addr
, dummy
;
297 struct dcss_segment
*seg
;
300 start_addr
= end_addr
= 0;
301 seg
= kmalloc(sizeof(*seg
), GFP_KERNEL
| GFP_DMA
);
306 dcss_mkname (name
, seg
->dcss_name
);
307 rc
= query_segment_type (seg
);
311 if (segment_overlaps_others(seg
)) {
316 rc
= vmem_add_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
321 seg
->res
= kzalloc(sizeof(struct resource
), GFP_KERNEL
);
322 if (seg
->res
== NULL
) {
326 seg
->res
->flags
= IORESOURCE_BUSY
| IORESOURCE_MEM
;
327 seg
->res
->start
= seg
->start_addr
;
328 seg
->res
->end
= seg
->end
;
329 memcpy(&seg
->res_name
, seg
->dcss_name
, 8);
330 EBCASC(seg
->res_name
, 8);
331 seg
->res_name
[8] = '\0';
332 strlcat(seg
->res_name
, " (DCSS)", sizeof(seg
->res_name
));
333 seg
->res
->name
= seg
->res_name
;
334 rc
= seg
->vm_segtype
;
335 if (rc
== SEG_TYPE_SC
||
336 ((rc
== SEG_TYPE_SR
|| rc
== SEG_TYPE_ER
) && !do_nonshared
))
337 seg
->res
->flags
|= IORESOURCE_READONLY
;
338 if (request_resource(&iomem_resource
, seg
->res
)) {
345 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
346 &start_addr
, &end_addr
);
348 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
349 &start_addr
, &end_addr
);
351 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
357 pr_warn("Loading DCSS %s failed with rc=%ld\n", name
, end_addr
);
358 rc
= dcss_diag_translate_rc(end_addr
);
359 dcss_diag(&purgeseg_scode
, seg
->dcss_name
,
363 seg
->start_addr
= start_addr
;
365 seg
->do_nonshared
= do_nonshared
;
366 refcount_set(&seg
->ref_count
, 1);
367 list_add(&seg
->list
, &dcss_list
);
368 *addr
= seg
->start_addr
;
371 pr_info("DCSS %s of range %px to %px and type %s loaded as "
372 "exclusive-writable\n", name
, (void*) seg
->start_addr
,
373 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
375 pr_info("DCSS %s of range %px to %px and type %s loaded in "
376 "shared access mode\n", name
, (void*) seg
->start_addr
,
377 (void*) seg
->end
, segtype_string
[seg
->vm_segtype
]);
381 release_resource(seg
->res
);
384 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
392 * this function loads a DCSS segment
393 * name : name of the DCSS
394 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
395 * 1 indicates that the dcss should be exclusive for this linux image
396 * addr : will be filled with start address of the segment
397 * end : will be filled with end address of the segment
399 * -ENOSYS : we are not running on VM
400 * -EIO : could not perform query or load diagnose
401 * -ENOENT : no such segment
402 * -EOPNOTSUPP: multi-part segment cannot be used with linux
403 * -ENOSPC : segment cannot be used (overlaps with storage)
404 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
405 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
406 * -EPERM : segment is currently loaded with incompatible permissions
407 * -ENOMEM : out of memory
408 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
411 segment_load (char *name
, int do_nonshared
, unsigned long *addr
,
414 struct dcss_segment
*seg
;
420 mutex_lock(&dcss_lock
);
421 seg
= segment_by_name (name
);
423 rc
= __segment_load (name
, do_nonshared
, addr
, end
);
425 if (do_nonshared
== seg
->do_nonshared
) {
426 refcount_inc(&seg
->ref_count
);
427 *addr
= seg
->start_addr
;
429 rc
= seg
->vm_segtype
;
435 mutex_unlock(&dcss_lock
);
440 * this function modifies the shared state of a DCSS segment. note that
441 * name : name of the DCSS
442 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
443 * 1 indicates that the dcss should be exclusive for this linux image
445 * -EIO : could not perform load diagnose (segment gone!)
446 * -ENOENT : no such segment (segment gone!)
447 * -EAGAIN : segment is in use by other exploiters, try later
448 * -EINVAL : no segment with the given name is currently loaded - name invalid
449 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
450 * 0 : operation succeeded
453 segment_modify_shared (char *name
, int do_nonshared
)
455 struct dcss_segment
*seg
;
456 unsigned long start_addr
, end_addr
, dummy
;
459 start_addr
= end_addr
= 0;
460 mutex_lock(&dcss_lock
);
461 seg
= segment_by_name (name
);
466 if (do_nonshared
== seg
->do_nonshared
) {
467 pr_info("DCSS %s is already in the requested access "
472 if (refcount_read(&seg
->ref_count
) != 1) {
473 pr_warn("DCSS %s is in use and cannot be reloaded\n", name
);
477 release_resource(seg
->res
);
479 seg
->res
->flags
&= ~IORESOURCE_READONLY
;
481 if (seg
->vm_segtype
== SEG_TYPE_SR
||
482 seg
->vm_segtype
== SEG_TYPE_ER
)
483 seg
->res
->flags
|= IORESOURCE_READONLY
;
485 if (request_resource(&iomem_resource
, seg
->res
)) {
486 pr_warn("DCSS %s overlaps with used memory resources and cannot be reloaded\n",
493 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
495 diag_cc
= dcss_diag(&loadnsr_scode
, seg
->dcss_name
,
496 &start_addr
, &end_addr
);
498 diag_cc
= dcss_diag(&loadshr_scode
, seg
->dcss_name
,
499 &start_addr
, &end_addr
);
505 pr_warn("Reloading DCSS %s failed with rc=%ld\n",
507 rc
= dcss_diag_translate_rc(end_addr
);
510 seg
->start_addr
= start_addr
;
512 seg
->do_nonshared
= do_nonshared
;
516 release_resource(seg
->res
);
519 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
520 list_del(&seg
->list
);
521 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
524 mutex_unlock(&dcss_lock
);
529 * Decrease the use count of a DCSS segment and remove
530 * it from the address space if nobody is using it
534 segment_unload(char *name
)
537 struct dcss_segment
*seg
;
542 mutex_lock(&dcss_lock
);
543 seg
= segment_by_name (name
);
545 pr_err("Unloading unknown DCSS %s failed\n", name
);
548 if (!refcount_dec_and_test(&seg
->ref_count
))
550 release_resource(seg
->res
);
552 vmem_remove_mapping(seg
->start_addr
, seg
->end
- seg
->start_addr
+ 1);
553 list_del(&seg
->list
);
554 dcss_diag(&purgeseg_scode
, seg
->dcss_name
, &dummy
, &dummy
);
557 mutex_unlock(&dcss_lock
);
561 * save segment content permanently
564 segment_save(char *name
)
566 struct dcss_segment
*seg
;
574 mutex_lock(&dcss_lock
);
575 seg
= segment_by_name (name
);
578 pr_err("Saving unknown DCSS %s failed\n", name
);
582 sprintf(cmd1
, "DEFSEG %s", name
);
583 for (i
=0; i
<seg
->segcnt
; i
++) {
584 sprintf(cmd1
+strlen(cmd1
), " %lX-%lX %s",
585 seg
->range
[i
].start
>> PAGE_SHIFT
,
586 seg
->range
[i
].end
>> PAGE_SHIFT
,
587 segtype_string
[seg
->range
[i
].start
& 0xff]);
589 sprintf(cmd2
, "SAVESEG %s", name
);
591 cpcmd(cmd1
, NULL
, 0, &response
);
593 pr_err("Saving a DCSS failed with DEFSEG response code "
597 cpcmd(cmd2
, NULL
, 0, &response
);
599 pr_err("Saving a DCSS failed with SAVESEG response code "
604 mutex_unlock(&dcss_lock
);
608 * print appropriate error message for segment_load()/segment_type()
611 void segment_warning(int rc
, char *seg_name
)
615 pr_err("DCSS %s cannot be loaded or queried\n", seg_name
);
618 pr_err("DCSS %s cannot be loaded or queried without "
622 pr_err("Loading or querying DCSS %s resulted in a "
623 "hardware error\n", seg_name
);
626 pr_err("DCSS %s has multiple page ranges and cannot be "
627 "loaded or queried\n", seg_name
);
630 pr_err("DCSS %s overlaps with used storage and cannot "
631 "be loaded\n", seg_name
);
634 pr_err("%s needs used memory resources and cannot be "
635 "loaded or queried\n", seg_name
);
638 pr_err("DCSS %s is already loaded in a different access "
642 pr_err("There is not enough memory to load or query "
643 "DCSS %s\n", seg_name
);
646 pr_err("DCSS %s exceeds the kernel mapping range (%lu) "
647 "and cannot be loaded\n", seg_name
, VMEM_MAX_PHYS
);
654 EXPORT_SYMBOL(segment_load
);
655 EXPORT_SYMBOL(segment_unload
);
656 EXPORT_SYMBOL(segment_save
);
657 EXPORT_SYMBOL(segment_type
);
658 EXPORT_SYMBOL(segment_modify_shared
);
659 EXPORT_SYMBOL(segment_warning
);