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>
18 #include <asm/ebcdic.h>
19 #include <asm/errno.h>
20 #include <asm/extmem.h>
21 #include <asm/cpcmd.h>
22 #include <linux/ctype.h>
24 #define DCSS_DEBUG /* Debug messages on/off */
26 #define DCSS_NAME "extmem"
28 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
30 #define PRINT_DEBUG(x...) do {} while (0)
32 #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
33 #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
34 #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
37 #define DCSS_LOADSHR 0x00
38 #define DCSS_LOADNSR 0x04
39 #define DCSS_PURGESEG 0x08
40 #define DCSS_FINDSEG 0x0c
41 #define DCSS_LOADNOLY 0x10
42 #define DCSS_SEGEXT 0x18
43 #define DCSS_FINDSEGA 0x0c
46 unsigned int start
; // 3byte start address, 1 byte type
47 unsigned int end
; // 3byte end address, 1 byte reserved
55 struct qrange range
[6];
69 struct list_head list
;
71 unsigned long start_addr
;
75 unsigned int vm_segtype
;
76 struct qrange range
[6];
80 static DEFINE_SPINLOCK(dcss_lock
);
81 static struct list_head dcss_list
= LIST_HEAD_INIT(dcss_list
);
82 static char *segtype_string
[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
86 unsigned long addr
, size
, type
;
87 } memory_chunk
[MEMORY_CHUNKS
];
90 * Create the 8 bytes, ebcdic VM segment name from
94 dcss_mkname(char *name
, char *dcss_name
)
98 for (i
= 0; i
< 8; i
++) {
101 dcss_name
[i
] = toupper(name
[i
]);
105 ASCEBC(dcss_name
, 8);
110 * search all segments in dcss_list, and return the one
111 * namend *name. If not found, return NULL.
113 static struct dcss_segment
*
114 segment_by_name (char *name
)
118 struct dcss_segment
*tmp
, *retval
= NULL
;
120 assert_spin_locked(&dcss_lock
);
121 dcss_mkname (name
, dcss_name
);
122 list_for_each (l
, &dcss_list
) {
123 tmp
= list_entry (l
, struct dcss_segment
, list
);
124 if (memcmp(tmp
->dcss_name
, dcss_name
, 8) == 0) {
134 * Perform a function on a dcss segment.
137 dcss_diag (__u8 func
, void *parameter
,
138 unsigned long *ret1
, unsigned long *ret2
)
140 unsigned long rx
, ry
;
143 rx
= (unsigned long) parameter
;
144 ry
= (unsigned long) func
;
145 __asm__
__volatile__(
146 #ifdef CONFIG_ARCH_S390X
147 " sam31\n" // switch to 31 bit
149 " sam64\n" // switch back to 64 bit
155 : "+d" (rx
), "+d" (ry
), "=d" (rc
) : : "cc" );
162 dcss_diag_translate_rc (int vm_rc
) {
169 /* do a diag to get info about a segment.
170 * fills start_address, end and vm_segtype fields
173 query_segment_type (struct dcss_segment
*seg
)
175 struct qin64
*qin
= kmalloc (sizeof(struct qin64
), GFP_DMA
);
176 struct qout64
*qout
= kmalloc (sizeof(struct qout64
), GFP_DMA
);
179 unsigned long dummy
, vmrc
;
181 if ((qin
== NULL
) || (qout
== NULL
)) {
186 /* initialize diag input parameters */
187 qin
->qopcode
= DCSS_FINDSEGA
;
188 qin
->qoutptr
= (unsigned long) qout
;
189 qin
->qoutlen
= sizeof(struct qout64
);
190 memcpy (qin
->qname
, seg
->dcss_name
, 8);
192 diag_cc
= dcss_diag (DCSS_SEGEXT
, qin
, &dummy
, &vmrc
);
195 rc
= dcss_diag_translate_rc (vmrc
);
199 if (qout
->segcnt
> 6) {
204 if (qout
->segcnt
== 1) {
205 seg
->vm_segtype
= qout
->range
[0].start
& 0xff;
207 /* multi-part segment. only one type supported here:
208 - all parts are contiguous
209 - all parts are either EW or EN type
210 - maximum 6 parts allowed */
211 unsigned long start
= qout
->segstart
>> PAGE_SHIFT
;
212 for (i
=0; i
<qout
->segcnt
; i
++) {
213 if (((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EW
) &&
214 ((qout
->range
[i
].start
& 0xff) != SEG_TYPE_EN
)) {
218 if (start
!= qout
->range
[i
].start
>> PAGE_SHIFT
) {
222 start
= (qout
->range
[i
].end
>> PAGE_SHIFT
) + 1;
224 seg
->vm_segtype
= SEG_TYPE_EWEN
;
227 /* analyze diag output and update seg */
228 seg
->start_addr
= qout
->segstart
;
229 seg
->end
= qout
->segend
;
231 memcpy (seg
->range
, qout
->range
, 6*sizeof(struct qrange
));
232 seg
->segcnt
= qout
->segcnt
;
238 if (qout
) kfree(qout
);
243 * check if the given segment collides with guest storage.
244 * returns 1 if this is the case, 0 if no collision was found
247 segment_overlaps_storage(struct dcss_segment
*seg
)
251 for (i
=0; i
< MEMORY_CHUNKS
&& memory_chunk
[i
].size
> 0; i
++) {
252 if (memory_chunk
[i
].type
!= 0)
254 if ((memory_chunk
[i
].addr
>> 20) > (seg
->end
>> 20))
256 if (((memory_chunk
[i
].addr
+ memory_chunk
[i
].size
- 1) >> 20)
257 < (seg
->start_addr
>> 20))
265 * check if segment collides with other segments that are currently loaded
266 * returns 1 if this is the case, 0 if no collision was found
269 segment_overlaps_others (struct dcss_segment
*seg
)
272 struct dcss_segment
*tmp
;
274 assert_spin_locked(&dcss_lock
);
275 list_for_each(l
, &dcss_list
) {
276 tmp
= list_entry(l
, struct dcss_segment
, list
);
277 if ((tmp
->start_addr
>> 20) > (seg
->end
>> 20))
279 if ((tmp
->end
>> 20) < (seg
->start_addr
>> 20))
289 * check if segment exceeds the kernel mapping range (detected or set via mem=)
290 * returns 1 if this is the case, 0 if segment fits into the range
293 segment_exceeds_range (struct dcss_segment
*seg
)
295 int seg_last_pfn
= (seg
->end
) >> PAGE_SHIFT
;
296 if (seg_last_pfn
> max_pfn
)
302 * get info about a segment
303 * possible return values:
304 * -ENOSYS : we are not running on VM
305 * -EIO : could not perform query diagnose
306 * -ENOENT : no such segment
307 * -ENOTSUPP: multi-part segment cannot be used with linux
308 * -ENOSPC : segment cannot be used (overlaps with storage)
309 * -ENOMEM : out of memory
310 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
313 segment_type (char* name
)
316 struct dcss_segment seg
;
321 dcss_mkname(name
, seg
.dcss_name
);
322 rc
= query_segment_type (&seg
);
325 return seg
.vm_segtype
;
329 * real segment loading function, called from segment_load
332 __segment_load (char *name
, int do_nonshared
, unsigned long *addr
, unsigned long *end
)
334 struct dcss_segment
*seg
= kmalloc(sizeof(struct dcss_segment
),
336 int dcss_command
, rc
, diag_cc
;
342 dcss_mkname (name
, seg
->dcss_name
);
343 rc
= query_segment_type (seg
);
346 if (segment_exceeds_range(seg
)) {
347 PRINT_WARN ("segment_load: not loading segment %s - exceeds"
348 " kernel mapping range\n",name
);
352 if (segment_overlaps_storage(seg
)) {
353 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
358 if (segment_overlaps_others(seg
)) {
359 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
360 " other segments\n",name
);
365 dcss_command
= DCSS_LOADNSR
;
367 dcss_command
= DCSS_LOADNOLY
;
369 diag_cc
= dcss_diag(dcss_command
, seg
->dcss_name
,
370 &seg
->start_addr
, &seg
->end
);
372 PRINT_WARN ("segment_load: could not load segment %s - "
373 "diag returned error (%ld)\n",name
,seg
->end
);
374 rc
= dcss_diag_translate_rc (seg
->end
);
375 dcss_diag(DCSS_PURGESEG
, seg
->dcss_name
,
376 &seg
->start_addr
, &seg
->end
);
379 seg
->do_nonshared
= do_nonshared
;
380 atomic_set(&seg
->ref_count
, 1);
381 list_add(&seg
->list
, &dcss_list
);
382 rc
= seg
->vm_segtype
;
383 *addr
= seg
->start_addr
;
386 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
387 "type %s in non-shared mode\n", name
,
388 (void*)seg
->start_addr
, (void*)seg
->end
,
389 segtype_string
[seg
->vm_segtype
]);
391 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
392 "type %s in shared mode\n", name
,
393 (void*)seg
->start_addr
, (void*)seg
->end
,
394 segtype_string
[seg
->vm_segtype
]);
403 * this function loads a DCSS segment
404 * name : name of the DCSS
405 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
406 * 1 indicates that the dcss should be exclusive for this linux image
407 * addr : will be filled with start address of the segment
408 * end : will be filled with end address of the segment
410 * -ENOSYS : we are not running on VM
411 * -EIO : could not perform query or load diagnose
412 * -ENOENT : no such segment
413 * -ENOTSUPP: multi-part segment cannot be used with linux
414 * -ENOSPC : segment cannot be used (overlaps with storage)
415 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
416 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
417 * -EPERM : segment is currently loaded with incompatible permissions
418 * -ENOMEM : out of memory
419 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
422 segment_load (char *name
, int do_nonshared
, unsigned long *addr
,
425 struct dcss_segment
*seg
;
431 spin_lock (&dcss_lock
);
432 seg
= segment_by_name (name
);
434 rc
= __segment_load (name
, do_nonshared
, addr
, end
);
436 if (do_nonshared
== seg
->do_nonshared
) {
437 atomic_inc(&seg
->ref_count
);
438 *addr
= seg
->start_addr
;
440 rc
= seg
->vm_segtype
;
446 spin_unlock (&dcss_lock
);
451 * this function modifies the shared state of a DCSS segment. note that
452 * name : name of the DCSS
453 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
454 * 1 indicates that the dcss should be exclusive for this linux image
456 * -EIO : could not perform load diagnose (segment gone!)
457 * -ENOENT : no such segment (segment gone!)
458 * -EAGAIN : segment is in use by other exploiters, try later
459 * -EINVAL : no segment with the given name is currently loaded - name invalid
460 * 0 : operation succeeded
463 segment_modify_shared (char *name
, int do_nonshared
)
465 struct dcss_segment
*seg
;
467 int dcss_command
, rc
, diag_cc
;
469 spin_lock (&dcss_lock
);
470 seg
= segment_by_name (name
);
475 if (do_nonshared
== seg
->do_nonshared
) {
476 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
477 " - already in requested mode\n",name
);
481 if (atomic_read (&seg
->ref_count
) != 1) {
482 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
483 "segment is in use by other driver(s)\n",name
);
487 dcss_diag(DCSS_PURGESEG
, seg
->dcss_name
,
490 dcss_command
= DCSS_LOADNSR
;
492 dcss_command
= DCSS_LOADNOLY
;
493 diag_cc
= dcss_diag(dcss_command
, seg
->dcss_name
,
494 &seg
->start_addr
, &seg
->end
);
496 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
497 " - diag returned error (%ld)\n",name
,seg
->end
);
498 rc
= dcss_diag_translate_rc (seg
->end
);
501 seg
->do_nonshared
= do_nonshared
;
505 list_del(&seg
->list
);
506 dcss_diag(DCSS_PURGESEG
, seg
->dcss_name
,
510 spin_unlock(&dcss_lock
);
515 * Decrease the use count of a DCSS segment and remove
516 * it from the address space if nobody is using it
520 segment_unload(char *name
)
523 struct dcss_segment
*seg
;
528 spin_lock(&dcss_lock
);
529 seg
= segment_by_name (name
);
531 PRINT_ERR ("could not find segment %s in segment_unload, "
532 "please report to linux390@de.ibm.com\n",name
);
535 if (atomic_dec_return(&seg
->ref_count
) == 0) {
536 list_del(&seg
->list
);
537 dcss_diag(DCSS_PURGESEG
, seg
->dcss_name
,
542 spin_unlock(&dcss_lock
);
546 * save segment content permanently
549 segment_save(char *name
)
551 struct dcss_segment
*seg
;
561 spin_lock(&dcss_lock
);
562 seg
= segment_by_name (name
);
565 PRINT_ERR ("could not find segment %s in segment_save, please report to linux390@de.ibm.com\n",name
);
569 startpfn
= seg
->start_addr
>> PAGE_SHIFT
;
570 endpfn
= (seg
->end
) >> PAGE_SHIFT
;
571 sprintf(cmd1
, "DEFSEG %s", name
);
572 for (i
=0; i
<seg
->segcnt
; i
++) {
573 sprintf(cmd1
+strlen(cmd1
), " %X-%X %s",
574 seg
->range
[i
].start
>> PAGE_SHIFT
,
575 seg
->range
[i
].end
>> PAGE_SHIFT
,
576 segtype_string
[seg
->range
[i
].start
& 0xff]);
578 sprintf(cmd2
, "SAVESEG %s", name
);
579 cpcmd(cmd1
, NULL
, 0);
580 cpcmd(cmd2
, NULL
, 0);
581 spin_unlock(&dcss_lock
);
584 EXPORT_SYMBOL(segment_load
);
585 EXPORT_SYMBOL(segment_unload
);
586 EXPORT_SYMBOL(segment_save
);
587 EXPORT_SYMBOL(segment_type
);
588 EXPORT_SYMBOL(segment_modify_shared
);