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 #include <linux/kernel.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/bootmem.h>
17 #include <linux/ctype.h>
19 #include <asm/ebcdic.h>
20 #include <asm/errno.h>
21 #include <asm/extmem.h>
22 #include <asm/cpcmd.h>
23 #include <asm/setup.h>
25 #define DCSS_DEBUG /* Debug messages on/off */
27 #define DCSS_NAME "extmem"
29 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
31 #define PRINT_DEBUG(x...) do {} while (0)
33 #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
34 #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
35 #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
38 #define DCSS_LOADSHR 0x00
39 #define DCSS_LOADNSR 0x04
40 #define DCSS_PURGESEG 0x08
41 #define DCSS_FINDSEG 0x0c
42 #define DCSS_LOADNOLY 0x10
43 #define DCSS_SEGEXT 0x18
44 #define DCSS_FINDSEGA 0x0c
47 unsigned int start; // 3byte start address, 1 byte type
48 unsigned int end; // 3byte end address, 1 byte reserved
56 struct qrange range[6];
70 struct list_head list;
72 unsigned long start_addr;
76 unsigned int vm_segtype;
77 struct qrange range[6];
81 static DEFINE_SPINLOCK(dcss_lock);
82 static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
83 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
87 * Create the 8 bytes, ebcdic VM segment name from
91 dcss_mkname(char *name, char *dcss_name)
95 for (i = 0; i < 8; i++) {
98 dcss_name[i] = toupper(name[i]);
102 ASCEBC(dcss_name, 8);
107 * search all segments in dcss_list, and return the one
108 * namend *name. If not found, return NULL.
110 static struct dcss_segment *
111 segment_by_name (char *name)
115 struct dcss_segment *tmp, *retval = NULL;
117 assert_spin_locked(&dcss_lock);
118 dcss_mkname (name, dcss_name);
119 list_for_each (l, &dcss_list) {
120 tmp = list_entry (l, struct dcss_segment, list);
121 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
131 * Perform a function on a dcss segment.
134 dcss_diag (__u8 func, void *parameter,
135 unsigned long *ret1, unsigned long *ret2)
137 unsigned long rx, ry;
140 rx = (unsigned long) parameter;
141 ry = (unsigned long) func;
152 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
159 dcss_diag_translate_rc (int vm_rc) {
166 /* do a diag to get info about a segment.
167 * fills start_address, end and vm_segtype fields
170 query_segment_type (struct dcss_segment *seg)
172 struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
173 struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
176 unsigned long dummy, vmrc;
178 if ((qin == NULL) || (qout == NULL)) {
183 /* initialize diag input parameters */
184 qin->qopcode = DCSS_FINDSEGA;
185 qin->qoutptr = (unsigned long) qout;
186 qin->qoutlen = sizeof(struct qout64);
187 memcpy (qin->qname, seg->dcss_name, 8);
189 diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
192 PRINT_WARN ("segment_type: diag returned error %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 * check if the given segment collides with guest storage.
242 * returns 1 if this is the case, 0 if no collision was found
245 segment_overlaps_storage(struct dcss_segment *seg)
249 for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
250 if (memory_chunk[i].type != CHUNK_READ_WRITE)
252 if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
254 if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
255 < (seg->start_addr >> 20))
263 * check if segment collides with other segments that are currently loaded
264 * returns 1 if this is the case, 0 if no collision was found
267 segment_overlaps_others (struct dcss_segment *seg)
270 struct dcss_segment *tmp;
272 assert_spin_locked(&dcss_lock);
273 list_for_each(l, &dcss_list) {
274 tmp = list_entry(l, struct dcss_segment, list);
275 if ((tmp->start_addr >> 20) > (seg->end >> 20))
277 if ((tmp->end >> 20) < (seg->start_addr >> 20))
287 * check if segment exceeds the kernel mapping range (detected or set via mem=)
288 * returns 1 if this is the case, 0 if segment fits into the range
291 segment_exceeds_range (struct dcss_segment *seg)
293 int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
294 if (seg_last_pfn > max_pfn)
300 * get info about a segment
301 * possible return values:
302 * -ENOSYS : we are not running on VM
303 * -EIO : could not perform query diagnose
304 * -ENOENT : no such segment
305 * -ENOTSUPP: multi-part segment cannot be used with linux
306 * -ENOSPC : segment cannot be used (overlaps with storage)
307 * -ENOMEM : out of memory
308 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
311 segment_type (char* name)
314 struct dcss_segment seg;
319 dcss_mkname(name, seg.dcss_name);
320 rc = query_segment_type (&seg);
323 return seg.vm_segtype;
327 * real segment loading function, called from segment_load
330 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
332 struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
334 int dcss_command, rc, diag_cc;
340 dcss_mkname (name, seg->dcss_name);
341 rc = query_segment_type (seg);
344 if (segment_exceeds_range(seg)) {
345 PRINT_WARN ("segment_load: not loading segment %s - exceeds"
346 " kernel mapping range\n",name);
350 if (segment_overlaps_storage(seg)) {
351 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
356 if (segment_overlaps_others(seg)) {
357 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
358 " other segments\n",name);
363 dcss_command = DCSS_LOADNSR;
365 dcss_command = DCSS_LOADNOLY;
367 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
368 &seg->start_addr, &seg->end);
370 PRINT_WARN ("segment_load: could not load segment %s - "
371 "diag returned error (%ld)\n",name,seg->end);
372 rc = dcss_diag_translate_rc (seg->end);
373 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
374 &seg->start_addr, &seg->end);
377 seg->do_nonshared = do_nonshared;
378 atomic_set(&seg->ref_count, 1);
379 list_add(&seg->list, &dcss_list);
380 rc = seg->vm_segtype;
381 *addr = seg->start_addr;
384 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
385 "type %s in non-shared mode\n", name,
386 (void*)seg->start_addr, (void*)seg->end,
387 segtype_string[seg->vm_segtype]);
389 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
390 "type %s in shared mode\n", name,
391 (void*)seg->start_addr, (void*)seg->end,
392 segtype_string[seg->vm_segtype]);
401 * this function loads a DCSS segment
402 * name : name of the DCSS
403 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
404 * 1 indicates that the dcss should be exclusive for this linux image
405 * addr : will be filled with start address of the segment
406 * end : will be filled with end address of the segment
408 * -ENOSYS : we are not running on VM
409 * -EIO : could not perform query or load diagnose
410 * -ENOENT : no such segment
411 * -ENOTSUPP: multi-part segment cannot be used with linux
412 * -ENOSPC : segment cannot be used (overlaps with storage)
413 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
414 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
415 * -EPERM : segment is currently loaded with incompatible permissions
416 * -ENOMEM : out of memory
417 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
420 segment_load (char *name, int do_nonshared, unsigned long *addr,
423 struct dcss_segment *seg;
429 spin_lock (&dcss_lock);
430 seg = segment_by_name (name);
432 rc = __segment_load (name, do_nonshared, addr, end);
434 if (do_nonshared == seg->do_nonshared) {
435 atomic_inc(&seg->ref_count);
436 *addr = seg->start_addr;
438 rc = seg->vm_segtype;
444 spin_unlock (&dcss_lock);
449 * this function modifies the shared state of a DCSS segment. note that
450 * name : name of the DCSS
451 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
452 * 1 indicates that the dcss should be exclusive for this linux image
454 * -EIO : could not perform load diagnose (segment gone!)
455 * -ENOENT : no such segment (segment gone!)
456 * -EAGAIN : segment is in use by other exploiters, try later
457 * -EINVAL : no segment with the given name is currently loaded - name invalid
458 * 0 : operation succeeded
461 segment_modify_shared (char *name, int do_nonshared)
463 struct dcss_segment *seg;
465 int dcss_command, rc, diag_cc;
467 spin_lock (&dcss_lock);
468 seg = segment_by_name (name);
473 if (do_nonshared == seg->do_nonshared) {
474 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
475 " - already in requested mode\n",name);
479 if (atomic_read (&seg->ref_count) != 1) {
480 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
481 "segment is in use by other driver(s)\n",name);
485 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
488 dcss_command = DCSS_LOADNSR;
490 dcss_command = DCSS_LOADNOLY;
491 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
492 &seg->start_addr, &seg->end);
494 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
495 " - diag returned error (%ld)\n",name,seg->end);
496 rc = dcss_diag_translate_rc (seg->end);
499 seg->do_nonshared = do_nonshared;
503 list_del(&seg->list);
504 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
508 spin_unlock(&dcss_lock);
513 * Decrease the use count of a DCSS segment and remove
514 * it from the address space if nobody is using it
518 segment_unload(char *name)
521 struct dcss_segment *seg;
526 spin_lock(&dcss_lock);
527 seg = segment_by_name (name);
529 PRINT_ERR ("could not find segment %s in segment_unload, "
530 "please report to linux390@de.ibm.com\n",name);
533 if (atomic_dec_return(&seg->ref_count) == 0) {
534 list_del(&seg->list);
535 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
540 spin_unlock(&dcss_lock);
544 * save segment content permanently
547 segment_save(char *name)
549 struct dcss_segment *seg;
559 spin_lock(&dcss_lock);
560 seg = segment_by_name (name);
563 PRINT_ERR("could not find segment %s in segment_save, please "
564 "report to linux390@de.ibm.com\n", name);
568 startpfn = seg->start_addr >> PAGE_SHIFT;
569 endpfn = (seg->end) >> PAGE_SHIFT;
570 sprintf(cmd1, "DEFSEG %s", name);
571 for (i=0; i<seg->segcnt; i++) {
572 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
573 seg->range[i].start >> PAGE_SHIFT,
574 seg->range[i].end >> PAGE_SHIFT,
575 segtype_string[seg->range[i].start & 0xff]);
577 sprintf(cmd2, "SAVESEG %s", name);
579 cpcmd(cmd1, NULL, 0, &response);
581 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
585 cpcmd(cmd2, NULL, 0, &response);
587 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
592 spin_unlock(&dcss_lock);
595 EXPORT_SYMBOL(segment_load);
596 EXPORT_SYMBOL(segment_unload);
597 EXPORT_SYMBOL(segment_save);
598 EXPORT_SYMBOL(segment_type);
599 EXPORT_SYMBOL(segment_modify_shared);