Merge master.kernel.org:/pub/scm/linux/kernel/git/davej/cpufreq
[linux-2.6] / arch / s390 / mm / extmem.c
1 /*
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
8  */
9
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 <asm/page.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>
23
24 #define DCSS_DEBUG      /* Debug messages on/off */
25
26 #define DCSS_NAME "extmem"
27 #ifdef DCSS_DEBUG
28 #define PRINT_DEBUG(x...)       printk(KERN_DEBUG DCSS_NAME " debug:" x)
29 #else
30 #define PRINT_DEBUG(x...)   do {} while (0)
31 #endif
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)
35
36
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
44
45 struct qrange {
46         unsigned int  start; // 3byte start address, 1 byte type
47         unsigned int  end;   // 3byte end address, 1 byte reserved
48 };
49
50 struct qout64 {
51         int segstart;
52         int segend;
53         int segcnt;
54         int segrcnt;
55         struct qrange range[6];
56 };
57
58 struct qin64 {
59         char qopcode;
60         char rsrv1[3];
61         char qrcode;
62         char rsrv2[3];
63         char qname[8];
64         unsigned int qoutptr;
65         short int qoutlen;
66 };
67
68 struct dcss_segment {
69         struct list_head list;
70         char dcss_name[8];
71         unsigned long start_addr;
72         unsigned long end;
73         atomic_t ref_count;
74         int do_nonshared;
75         unsigned int vm_segtype;
76         struct qrange range[6];
77         int segcnt;
78 };
79
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",
83                                         "EW/EN-MIXED" };
84
85 extern struct {
86         unsigned long addr, size, type;
87 } memory_chunk[MEMORY_CHUNKS];
88
89 /*
90  * Create the 8 bytes, ebcdic VM segment name from
91  * an ascii name.
92  */
93 static void inline
94 dcss_mkname(char *name, char *dcss_name)
95 {
96         int i;
97
98         for (i = 0; i < 8; i++) {
99                 if (name[i] == '\0')
100                         break;
101                 dcss_name[i] = toupper(name[i]);
102         };
103         for (; i < 8; i++)
104                 dcss_name[i] = ' ';
105         ASCEBC(dcss_name, 8);
106 }
107
108
109 /*
110  * search all segments in dcss_list, and return the one
111  * namend *name. If not found, return NULL.
112  */
113 static struct dcss_segment *
114 segment_by_name (char *name)
115 {
116         char dcss_name[9];
117         struct list_head *l;
118         struct dcss_segment *tmp, *retval = NULL;
119
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) {
125                         retval = tmp;
126                         break;
127                 }
128         }
129         return retval;
130 }
131
132
133 /*
134  * Perform a function on a dcss segment.
135  */
136 static inline int
137 dcss_diag (__u8 func, void *parameter,
138            unsigned long *ret1, unsigned long *ret2)
139 {
140         unsigned long rx, ry;
141         int rc;
142
143         rx = (unsigned long) parameter;
144         ry = (unsigned long) func;
145         asm volatile(
146 #ifdef CONFIG_64BIT
147                 "       sam31\n"
148                 "       diag    %0,%1,0x64\n"
149                 "       sam64\n"
150 #else
151                 "       diag    %0,%1,0x64\n"
152 #endif
153                 "       ipm     %2\n"
154                 "       srl     %2,28\n"
155                 : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
156         *ret1 = rx;
157         *ret2 = ry;
158         return rc;
159 }
160
161 static inline int
162 dcss_diag_translate_rc (int vm_rc) {
163         if (vm_rc == 44)
164                 return -ENOENT;
165         return -EIO;
166 }
167
168
169 /* do a diag to get info about a segment.
170  * fills start_address, end and vm_segtype fields
171  */
172 static int
173 query_segment_type (struct dcss_segment *seg)
174 {
175         struct qin64  *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
176         struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
177
178         int diag_cc, rc, i;
179         unsigned long dummy, vmrc;
180
181         if ((qin == NULL) || (qout == NULL)) {
182                 rc = -ENOMEM;
183                 goto out_free;
184         }
185
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);
191
192         diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
193
194         if (diag_cc > 1) {
195                 PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
196                 rc = dcss_diag_translate_rc (vmrc);
197                 goto out_free;
198         }
199
200         if (qout->segcnt > 6) {
201                 rc = -ENOTSUPP;
202                 goto out_free;
203         }
204
205         if (qout->segcnt == 1) {
206                 seg->vm_segtype = qout->range[0].start & 0xff;
207         } else {
208                 /* multi-part segment. only one type supported here:
209                     - all parts are contiguous
210                     - all parts are either EW or EN type
211                     - maximum 6 parts allowed */
212                 unsigned long start = qout->segstart >> PAGE_SHIFT;
213                 for (i=0; i<qout->segcnt; i++) {
214                         if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
215                             ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
216                                 rc = -ENOTSUPP;
217                                 goto out_free;
218                         }
219                         if (start != qout->range[i].start >> PAGE_SHIFT) {
220                                 rc = -ENOTSUPP;
221                                 goto out_free;
222                         }
223                         start = (qout->range[i].end >> PAGE_SHIFT) + 1;
224                 }
225                 seg->vm_segtype = SEG_TYPE_EWEN;
226         }
227
228         /* analyze diag output and update seg */
229         seg->start_addr = qout->segstart;
230         seg->end = qout->segend;
231
232         memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
233         seg->segcnt = qout->segcnt;
234
235         rc = 0;
236
237  out_free:
238         kfree(qin);
239         kfree(qout);
240         return rc;
241 }
242
243 /*
244  * check if the given segment collides with guest storage.
245  * returns 1 if this is the case, 0 if no collision was found
246  */
247 static int
248 segment_overlaps_storage(struct dcss_segment *seg)
249 {
250         int i;
251
252         for (i=0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
253                 if (memory_chunk[i].type != 0)
254                         continue;
255                 if ((memory_chunk[i].addr >> 20) > (seg->end >> 20))
256                         continue;
257                 if (((memory_chunk[i].addr + memory_chunk[i].size - 1) >> 20)
258                                 < (seg->start_addr >> 20))
259                         continue;
260                 return 1;
261         }
262         return 0;
263 }
264
265 /*
266  * check if segment collides with other segments that are currently loaded
267  * returns 1 if this is the case, 0 if no collision was found
268  */
269 static int
270 segment_overlaps_others (struct dcss_segment *seg)
271 {
272         struct list_head *l;
273         struct dcss_segment *tmp;
274
275         assert_spin_locked(&dcss_lock);
276         list_for_each(l, &dcss_list) {
277                 tmp = list_entry(l, struct dcss_segment, list);
278                 if ((tmp->start_addr >> 20) > (seg->end >> 20))
279                         continue;
280                 if ((tmp->end >> 20) < (seg->start_addr >> 20))
281                         continue;
282                 if (seg == tmp)
283                         continue;
284                 return 1;
285         }
286         return 0;
287 }
288
289 /*
290  * check if segment exceeds the kernel mapping range (detected or set via mem=)
291  * returns 1 if this is the case, 0 if segment fits into the range
292  */
293 static inline int
294 segment_exceeds_range (struct dcss_segment *seg)
295 {
296         int seg_last_pfn = (seg->end) >> PAGE_SHIFT;
297         if (seg_last_pfn > max_pfn)
298                 return 1;
299         return 0;
300 }
301
302 /*
303  * get info about a segment
304  * possible return values:
305  * -ENOSYS  : we are not running on VM
306  * -EIO     : could not perform query diagnose
307  * -ENOENT  : no such segment
308  * -ENOTSUPP: multi-part segment cannot be used with linux
309  * -ENOSPC  : segment cannot be used (overlaps with storage)
310  * -ENOMEM  : out of memory
311  * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
312  */
313 int
314 segment_type (char* name)
315 {
316         int rc;
317         struct dcss_segment seg;
318
319         if (!MACHINE_IS_VM)
320                 return -ENOSYS;
321
322         dcss_mkname(name, seg.dcss_name);
323         rc = query_segment_type (&seg);
324         if (rc < 0)
325                 return rc;
326         return seg.vm_segtype;
327 }
328
329 /*
330  * real segment loading function, called from segment_load
331  */
332 static int
333 __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
334 {
335         struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
336                         GFP_DMA);
337         int dcss_command, rc, diag_cc;
338
339         if (seg == NULL) {
340                 rc = -ENOMEM;
341                 goto out;
342         }
343         dcss_mkname (name, seg->dcss_name);
344         rc = query_segment_type (seg);
345         if (rc < 0)
346                 goto out_free;
347         if (segment_exceeds_range(seg)) {
348                 PRINT_WARN ("segment_load: not loading segment %s - exceeds"
349                                 " kernel mapping range\n",name);
350                 rc = -ERANGE;
351                 goto out_free;
352         }
353         if (segment_overlaps_storage(seg)) {
354                 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
355                                 " storage\n",name);
356                 rc = -ENOSPC;
357                 goto out_free;
358         }
359         if (segment_overlaps_others(seg)) {
360                 PRINT_WARN ("segment_load: not loading segment %s - overlaps"
361                                 " other segments\n",name);
362                 rc = -EBUSY;
363                 goto out_free;
364         }
365         if (do_nonshared)
366                 dcss_command = DCSS_LOADNSR;
367         else
368                 dcss_command = DCSS_LOADNOLY;
369
370         diag_cc = dcss_diag(dcss_command, seg->dcss_name,
371                         &seg->start_addr, &seg->end);
372         if (diag_cc > 1) {
373                 PRINT_WARN ("segment_load: could not load segment %s - "
374                                 "diag returned error (%ld)\n",name,seg->end);
375                 rc = dcss_diag_translate_rc (seg->end);
376                 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
377                                 &seg->start_addr, &seg->end);
378                 goto out_free;
379         }
380         seg->do_nonshared = do_nonshared;
381         atomic_set(&seg->ref_count, 1);
382         list_add(&seg->list, &dcss_list);
383         rc = seg->vm_segtype;
384         *addr = seg->start_addr;
385         *end  = seg->end;
386         if (do_nonshared)
387                 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
388                                 "type %s in non-shared mode\n", name,
389                                 (void*)seg->start_addr, (void*)seg->end,
390                                 segtype_string[seg->vm_segtype]);
391         else
392                 PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
393                                 "type %s in shared mode\n", name,
394                                 (void*)seg->start_addr, (void*)seg->end,
395                                 segtype_string[seg->vm_segtype]);
396         goto out;
397  out_free:
398         kfree(seg);
399  out:
400         return rc;
401 }
402
403 /*
404  * this function loads a DCSS segment
405  * name         : name of the DCSS
406  * do_nonshared : 0 indicates that the dcss should be shared with other linux images
407  *                1 indicates that the dcss should be exclusive for this linux image
408  * addr         : will be filled with start address of the segment
409  * end          : will be filled with end address of the segment
410  * return values:
411  * -ENOSYS  : we are not running on VM
412  * -EIO     : could not perform query or load diagnose
413  * -ENOENT  : no such segment
414  * -ENOTSUPP: multi-part segment cannot be used with linux
415  * -ENOSPC  : segment cannot be used (overlaps with storage)
416  * -EBUSY   : segment can temporarily not be used (overlaps with dcss)
417  * -ERANGE  : segment cannot be used (exceeds kernel mapping range)
418  * -EPERM   : segment is currently loaded with incompatible permissions
419  * -ENOMEM  : out of memory
420  * 0 .. 6   : type of segment as defined in include/asm-s390/extmem.h
421  */
422 int
423 segment_load (char *name, int do_nonshared, unsigned long *addr,
424                 unsigned long *end)
425 {
426         struct dcss_segment *seg;
427         int rc;
428
429         if (!MACHINE_IS_VM)
430                 return -ENOSYS;
431
432         spin_lock (&dcss_lock);
433         seg = segment_by_name (name);
434         if (seg == NULL)
435                 rc = __segment_load (name, do_nonshared, addr, end);
436         else {
437                 if (do_nonshared == seg->do_nonshared) {
438                         atomic_inc(&seg->ref_count);
439                         *addr = seg->start_addr;
440                         *end  = seg->end;
441                         rc    = seg->vm_segtype;
442                 } else {
443                         *addr = *end = 0;
444                         rc    = -EPERM;
445                 }
446         }
447         spin_unlock (&dcss_lock);
448         return rc;
449 }
450
451 /*
452  * this function modifies the shared state of a DCSS segment. note that
453  * name         : name of the DCSS
454  * do_nonshared : 0 indicates that the dcss should be shared with other linux images
455  *                1 indicates that the dcss should be exclusive for this linux image
456  * return values:
457  * -EIO     : could not perform load diagnose (segment gone!)
458  * -ENOENT  : no such segment (segment gone!)
459  * -EAGAIN  : segment is in use by other exploiters, try later
460  * -EINVAL  : no segment with the given name is currently loaded - name invalid
461  * 0        : operation succeeded
462  */
463 int
464 segment_modify_shared (char *name, int do_nonshared)
465 {
466         struct dcss_segment *seg;
467         unsigned long dummy;
468         int dcss_command, rc, diag_cc;
469
470         spin_lock (&dcss_lock);
471         seg = segment_by_name (name);
472         if (seg == NULL) {
473                 rc = -EINVAL;
474                 goto out_unlock;
475         }
476         if (do_nonshared == seg->do_nonshared) {
477                 PRINT_INFO ("segment_modify_shared: not reloading segment %s"
478                                 " - already in requested mode\n",name);
479                 rc = 0;
480                 goto out_unlock;
481         }
482         if (atomic_read (&seg->ref_count) != 1) {
483                 PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
484                                 "segment is in use by other driver(s)\n",name);
485                 rc = -EAGAIN;
486                 goto out_unlock;
487         }
488         dcss_diag(DCSS_PURGESEG, seg->dcss_name,
489                   &dummy, &dummy);
490         if (do_nonshared)
491                 dcss_command = DCSS_LOADNSR;
492         else
493         dcss_command = DCSS_LOADNOLY;
494         diag_cc = dcss_diag(dcss_command, seg->dcss_name,
495                         &seg->start_addr, &seg->end);
496         if (diag_cc > 1) {
497                 PRINT_WARN ("segment_modify_shared: could not reload segment %s"
498                                 " - diag returned error (%ld)\n",name,seg->end);
499                 rc = dcss_diag_translate_rc (seg->end);
500                 goto out_del;
501         }
502         seg->do_nonshared = do_nonshared;
503         rc = 0;
504         goto out_unlock;
505  out_del:
506         list_del(&seg->list);
507         dcss_diag(DCSS_PURGESEG, seg->dcss_name,
508                   &dummy, &dummy);
509         kfree(seg);
510  out_unlock:
511         spin_unlock(&dcss_lock);
512         return rc;
513 }
514
515 /*
516  * Decrease the use count of a DCSS segment and remove
517  * it from the address space if nobody is using it
518  * any longer.
519  */
520 void
521 segment_unload(char *name)
522 {
523         unsigned long dummy;
524         struct dcss_segment *seg;
525
526         if (!MACHINE_IS_VM)
527                 return;
528
529         spin_lock(&dcss_lock);
530         seg = segment_by_name (name);
531         if (seg == NULL) {
532                 PRINT_ERR ("could not find segment %s in segment_unload, "
533                                 "please report to linux390@de.ibm.com\n",name);
534                 goto out_unlock;
535         }
536         if (atomic_dec_return(&seg->ref_count) == 0) {
537                 list_del(&seg->list);
538                 dcss_diag(DCSS_PURGESEG, seg->dcss_name,
539                           &dummy, &dummy);
540                 kfree(seg);
541         }
542 out_unlock:
543         spin_unlock(&dcss_lock);
544 }
545
546 /*
547  * save segment content permanently
548  */
549 void
550 segment_save(char *name)
551 {
552         struct dcss_segment *seg;
553         int startpfn = 0;
554         int endpfn = 0;
555         char cmd1[160];
556         char cmd2[80];
557         int i, response;
558
559         if (!MACHINE_IS_VM)
560                 return;
561
562         spin_lock(&dcss_lock);
563         seg = segment_by_name (name);
564
565         if (seg == NULL) {
566                 PRINT_ERR ("could not find segment %s in segment_save, please report to linux390@de.ibm.com\n",name);
567                 return;
568         }
569
570         startpfn = seg->start_addr >> PAGE_SHIFT;
571         endpfn = (seg->end) >> PAGE_SHIFT;
572         sprintf(cmd1, "DEFSEG %s", name);
573         for (i=0; i<seg->segcnt; i++) {
574                 sprintf(cmd1+strlen(cmd1), " %X-%X %s",
575                         seg->range[i].start >> PAGE_SHIFT,
576                         seg->range[i].end >> PAGE_SHIFT,
577                         segtype_string[seg->range[i].start & 0xff]);
578         }
579         sprintf(cmd2, "SAVESEG %s", name);
580         response = 0;
581         cpcmd(cmd1, NULL, 0, &response);
582         if (response) {
583                 PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
584                           response);
585                 goto out;
586         }
587         cpcmd(cmd2, NULL, 0, &response);
588         if (response) {
589                 PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
590                           response);
591                 goto out;
592         }
593 out:
594         spin_unlock(&dcss_lock);
595 }
596
597 EXPORT_SYMBOL(segment_load);
598 EXPORT_SYMBOL(segment_unload);
599 EXPORT_SYMBOL(segment_save);
600 EXPORT_SYMBOL(segment_type);
601 EXPORT_SYMBOL(segment_modify_shared);