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/pgtable.h>
20 #include <asm/ebcdic.h>
21 #include <asm/errno.h>
22 #include <asm/extmem.h>
23 #include <asm/cpcmd.h>
24 #include <asm/setup.h>
26 #define DCSS_DEBUG /* Debug messages on/off */
28 #define DCSS_NAME "extmem"
30 #define PRINT_DEBUG(x...) printk(KERN_DEBUG DCSS_NAME " debug:" x)
32 #define PRINT_DEBUG(x...) do {} while (0)
34 #define PRINT_INFO(x...) printk(KERN_INFO DCSS_NAME " info:" x)
35 #define PRINT_WARN(x...) printk(KERN_WARNING DCSS_NAME " warning:" x)
36 #define PRINT_ERR(x...) printk(KERN_ERR DCSS_NAME " error:" x)
39 #define DCSS_LOADSHR 0x00
40 #define DCSS_LOADNSR 0x04
41 #define DCSS_PURGESEG 0x08
42 #define DCSS_FINDSEG 0x0c
43 #define DCSS_LOADNOLY 0x10
44 #define DCSS_SEGEXT 0x18
45 #define DCSS_FINDSEGA 0x0c
48 unsigned int start; // 3byte start address, 1 byte type
49 unsigned int end; // 3byte end address, 1 byte reserved
57 struct qrange range[6];
71 struct list_head list;
73 unsigned long start_addr;
77 unsigned int vm_segtype;
78 struct qrange range[6];
82 static DEFINE_MUTEX(dcss_lock);
83 static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
84 static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
88 * Create the 8 bytes, ebcdic VM segment name from
92 dcss_mkname(char *name, char *dcss_name)
96 for (i = 0; i < 8; i++) {
99 dcss_name[i] = toupper(name[i]);
103 ASCEBC(dcss_name, 8);
108 * search all segments in dcss_list, and return the one
109 * namend *name. If not found, return NULL.
111 static struct dcss_segment *
112 segment_by_name (char *name)
116 struct dcss_segment *tmp, *retval = NULL;
118 BUG_ON(!mutex_is_locked(&dcss_lock));
119 dcss_mkname (name, dcss_name);
120 list_for_each (l, &dcss_list) {
121 tmp = list_entry (l, struct dcss_segment, list);
122 if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
132 * Perform a function on a dcss segment.
135 dcss_diag (__u8 func, void *parameter,
136 unsigned long *ret1, unsigned long *ret2)
138 unsigned long rx, ry;
141 rx = (unsigned long) parameter;
142 ry = (unsigned long) func;
153 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
160 dcss_diag_translate_rc (int vm_rc) {
167 /* do a diag to get info about a segment.
168 * fills start_address, end and vm_segtype fields
171 query_segment_type (struct dcss_segment *seg)
173 struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
174 struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
177 unsigned long dummy, vmrc;
179 if ((qin == NULL) || (qout == NULL)) {
184 /* initialize diag input parameters */
185 qin->qopcode = DCSS_FINDSEGA;
186 qin->qoutptr = (unsigned long) qout;
187 qin->qoutlen = sizeof(struct qout64);
188 memcpy (qin->qname, seg->dcss_name, 8);
190 diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
193 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
194 rc = dcss_diag_translate_rc (vmrc);
198 if (qout->segcnt > 6) {
203 if (qout->segcnt == 1) {
204 seg->vm_segtype = qout->range[0].start & 0xff;
206 /* multi-part segment. only one type supported here:
207 - all parts are contiguous
208 - all parts are either EW or EN type
209 - maximum 6 parts allowed */
210 unsigned long start = qout->segstart >> PAGE_SHIFT;
211 for (i=0; i<qout->segcnt; i++) {
212 if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
213 ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
217 if (start != qout->range[i].start >> PAGE_SHIFT) {
221 start = (qout->range[i].end >> PAGE_SHIFT) + 1;
223 seg->vm_segtype = SEG_TYPE_EWEN;
226 /* analyze diag output and update seg */
227 seg->start_addr = qout->segstart;
228 seg->end = qout->segend;
230 memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
231 seg->segcnt = qout->segcnt;
242 * get info about a segment
243 * possible return values:
244 * -ENOSYS : we are not running on VM
245 * -EIO : could not perform query diagnose
246 * -ENOENT : no such segment
247 * -ENOTSUPP: multi-part segment cannot be used with linux
248 * -ENOSPC : segment cannot be used (overlaps with storage)
249 * -ENOMEM : out of memory
250 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
253 segment_type (char* name)
256 struct dcss_segment seg;
261 dcss_mkname(name, seg.dcss_name);
262 rc = query_segment_type (&seg);
265 return seg.vm_segtype;
269 * real segment loading function, called from segment_load
272 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
274 struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
276 int dcss_command, rc, diag_cc;
282 dcss_mkname (name, seg->dcss_name);
283 rc = query_segment_type (seg);
287 rc = add_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
293 PRINT_WARN("segment_load: not loading segment %s - overlaps "
294 "storage/segment\n", name);
297 PRINT_WARN("segment_load: not loading segment %s - exceeds "
298 "kernel mapping range\n", name);
301 PRINT_WARN("segment_load: not loading segment %s (rc: %d)\n",
307 dcss_command = DCSS_LOADNSR;
309 dcss_command = DCSS_LOADNOLY;
311 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
312 &seg->start_addr, &seg->end);
314 PRINT_WARN ("segment_load: could not load segment %s - "
315 "diag returned error (%ld)\n",name,seg->end);
316 rc = dcss_diag_translate_rc (seg->end);
317 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
318 &seg->start_addr, &seg->end);
321 seg->do_nonshared = do_nonshared;
322 atomic_set(&seg->ref_count, 1);
323 list_add(&seg->list, &dcss_list);
324 rc = seg->vm_segtype;
325 *addr = seg->start_addr;
328 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
329 "type %s in non-shared mode\n", name,
330 (void*)seg->start_addr, (void*)seg->end,
331 segtype_string[seg->vm_segtype]);
333 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
334 "type %s in shared mode\n", name,
335 (void*)seg->start_addr, (void*)seg->end,
336 segtype_string[seg->vm_segtype]);
339 remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
347 * this function loads a DCSS segment
348 * name : name of the DCSS
349 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
350 * 1 indicates that the dcss should be exclusive for this linux image
351 * addr : will be filled with start address of the segment
352 * end : will be filled with end address of the segment
354 * -ENOSYS : we are not running on VM
355 * -EIO : could not perform query or load diagnose
356 * -ENOENT : no such segment
357 * -ENOTSUPP: multi-part segment cannot be used with linux
358 * -ENOSPC : segment cannot be used (overlaps with storage)
359 * -EBUSY : segment can temporarily not be used (overlaps with dcss)
360 * -ERANGE : segment cannot be used (exceeds kernel mapping range)
361 * -EPERM : segment is currently loaded with incompatible permissions
362 * -ENOMEM : out of memory
363 * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
366 segment_load (char *name, int do_nonshared, unsigned long *addr,
369 struct dcss_segment *seg;
375 mutex_lock(&dcss_lock);
376 seg = segment_by_name (name);
378 rc = __segment_load (name, do_nonshared, addr, end);
380 if (do_nonshared == seg->do_nonshared) {
381 atomic_inc(&seg->ref_count);
382 *addr = seg->start_addr;
384 rc = seg->vm_segtype;
390 mutex_unlock(&dcss_lock);
395 * this function modifies the shared state of a DCSS segment. note that
396 * name : name of the DCSS
397 * do_nonshared : 0 indicates that the dcss should be shared with other linux images
398 * 1 indicates that the dcss should be exclusive for this linux image
400 * -EIO : could not perform load diagnose (segment gone!)
401 * -ENOENT : no such segment (segment gone!)
402 * -EAGAIN : segment is in use by other exploiters, try later
403 * -EINVAL : no segment with the given name is currently loaded - name invalid
404 * 0 : operation succeeded
407 segment_modify_shared (char *name, int do_nonshared)
409 struct dcss_segment *seg;
411 int dcss_command, rc, diag_cc;
413 mutex_lock(&dcss_lock);
414 seg = segment_by_name (name);
419 if (do_nonshared == seg->do_nonshared) {
420 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
421 " - already in requested mode\n",name);
425 if (atomic_read (&seg->ref_count) != 1) {
426 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
427 "segment is in use by other driver(s)\n",name);
431 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
434 dcss_command = DCSS_LOADNSR;
436 dcss_command = DCSS_LOADNOLY;
437 diag_cc = dcss_diag(dcss_command, seg->dcss_name,
438 &seg->start_addr, &seg->end);
440 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
441 " - diag returned error (%ld)\n",name,seg->end);
442 rc = dcss_diag_translate_rc (seg->end);
445 seg->do_nonshared = do_nonshared;
449 list_del(&seg->list);
450 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
454 mutex_unlock(&dcss_lock);
459 * Decrease the use count of a DCSS segment and remove
460 * it from the address space if nobody is using it
464 segment_unload(char *name)
467 struct dcss_segment *seg;
472 mutex_lock(&dcss_lock);
473 seg = segment_by_name (name);
475 PRINT_ERR ("could not find segment %s in segment_unload, "
476 "please report to linux390@de.ibm.com\n",name);
479 if (atomic_dec_return(&seg->ref_count) != 0)
481 remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
482 list_del(&seg->list);
483 dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
486 mutex_unlock(&dcss_lock);
490 * save segment content permanently
493 segment_save(char *name)
495 struct dcss_segment *seg;
505 mutex_lock(&dcss_lock);
506 seg = segment_by_name (name);
509 PRINT_ERR("could not find segment %s in segment_save, please "
510 "report to linux390@de.ibm.com\n", name);
514 startpfn = seg->start_addr >> PAGE_SHIFT;
515 endpfn = (seg->end) >> PAGE_SHIFT;
516 sprintf(cmd1, "DEFSEG %s", name);
517 for (i=0; i<seg->segcnt; i++) {
518 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
519 seg->range[i].start >> PAGE_SHIFT,
520 seg->range[i].end >> PAGE_SHIFT,
521 segtype_string[seg->range[i].start & 0xff]);
523 sprintf(cmd2, "SAVESEG %s", name);
525 cpcmd(cmd1, NULL, 0, &response);
527 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
531 cpcmd(cmd2, NULL, 0, &response);
533 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
538 mutex_unlock(&dcss_lock);
541 EXPORT_SYMBOL(segment_load);
542 EXPORT_SYMBOL(segment_unload);
543 EXPORT_SYMBOL(segment_save);
544 EXPORT_SYMBOL(segment_type);
545 EXPORT_SYMBOL(segment_modify_shared);