extmem.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. #include <linux/kernel.h>
  10. #include <linux/string.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/list.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/ctype.h>
  17. #include <linux/ioport.h>
  18. #include <asm/page.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>
  25. #define DCSS_DEBUG /* Debug messages on/off */
  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. #define DCSS_LOADSHR 0x00
  36. #define DCSS_LOADNSR 0x04
  37. #define DCSS_PURGESEG 0x08
  38. #define DCSS_FINDSEG 0x0c
  39. #define DCSS_LOADNOLY 0x10
  40. #define DCSS_SEGEXT 0x18
  41. #define DCSS_FINDSEGA 0x0c
  42. struct qrange {
  43. unsigned int start; // 3byte start address, 1 byte type
  44. unsigned int end; // 3byte end address, 1 byte reserved
  45. };
  46. struct qout64 {
  47. int segstart;
  48. int segend;
  49. int segcnt;
  50. int segrcnt;
  51. struct qrange range[6];
  52. };
  53. struct qin64 {
  54. char qopcode;
  55. char rsrv1[3];
  56. char qrcode;
  57. char rsrv2[3];
  58. char qname[8];
  59. unsigned int qoutptr;
  60. short int qoutlen;
  61. };
  62. struct dcss_segment {
  63. struct list_head list;
  64. char dcss_name[8];
  65. char res_name[15];
  66. unsigned long start_addr;
  67. unsigned long end;
  68. atomic_t ref_count;
  69. int do_nonshared;
  70. unsigned int vm_segtype;
  71. struct qrange range[6];
  72. int segcnt;
  73. struct resource *res;
  74. };
  75. static DEFINE_MUTEX(dcss_lock);
  76. static struct list_head dcss_list = LIST_HEAD_INIT(dcss_list);
  77. static char *segtype_string[] = { "SW", "EW", "SR", "ER", "SN", "EN", "SC",
  78. "EW/EN-MIXED" };
  79. /*
  80. * Create the 8 bytes, ebcdic VM segment name from
  81. * an ascii name.
  82. */
  83. static void
  84. dcss_mkname(char *name, char *dcss_name)
  85. {
  86. int i;
  87. for (i = 0; i < 8; i++) {
  88. if (name[i] == '\0')
  89. break;
  90. dcss_name[i] = toupper(name[i]);
  91. };
  92. for (; i < 8; i++)
  93. dcss_name[i] = ' ';
  94. ASCEBC(dcss_name, 8);
  95. }
  96. /*
  97. * search all segments in dcss_list, and return the one
  98. * namend *name. If not found, return NULL.
  99. */
  100. static struct dcss_segment *
  101. segment_by_name (char *name)
  102. {
  103. char dcss_name[9];
  104. struct list_head *l;
  105. struct dcss_segment *tmp, *retval = NULL;
  106. BUG_ON(!mutex_is_locked(&dcss_lock));
  107. dcss_mkname (name, dcss_name);
  108. list_for_each (l, &dcss_list) {
  109. tmp = list_entry (l, struct dcss_segment, list);
  110. if (memcmp(tmp->dcss_name, dcss_name, 8) == 0) {
  111. retval = tmp;
  112. break;
  113. }
  114. }
  115. return retval;
  116. }
  117. /*
  118. * Perform a function on a dcss segment.
  119. */
  120. static inline int
  121. dcss_diag (__u8 func, void *parameter,
  122. unsigned long *ret1, unsigned long *ret2)
  123. {
  124. unsigned long rx, ry;
  125. int rc;
  126. rx = (unsigned long) parameter;
  127. ry = (unsigned long) func;
  128. asm volatile(
  129. #ifdef CONFIG_64BIT
  130. " sam31\n"
  131. " diag %0,%1,0x64\n"
  132. " sam64\n"
  133. #else
  134. " diag %0,%1,0x64\n"
  135. #endif
  136. " ipm %2\n"
  137. " srl %2,28\n"
  138. : "+d" (rx), "+d" (ry), "=d" (rc) : : "cc");
  139. *ret1 = rx;
  140. *ret2 = ry;
  141. return rc;
  142. }
  143. static inline int
  144. dcss_diag_translate_rc (int vm_rc) {
  145. if (vm_rc == 44)
  146. return -ENOENT;
  147. return -EIO;
  148. }
  149. /* do a diag to get info about a segment.
  150. * fills start_address, end and vm_segtype fields
  151. */
  152. static int
  153. query_segment_type (struct dcss_segment *seg)
  154. {
  155. struct qin64 *qin = kmalloc (sizeof(struct qin64), GFP_DMA);
  156. struct qout64 *qout = kmalloc (sizeof(struct qout64), GFP_DMA);
  157. int diag_cc, rc, i;
  158. unsigned long dummy, vmrc;
  159. if ((qin == NULL) || (qout == NULL)) {
  160. rc = -ENOMEM;
  161. goto out_free;
  162. }
  163. /* initialize diag input parameters */
  164. qin->qopcode = DCSS_FINDSEGA;
  165. qin->qoutptr = (unsigned long) qout;
  166. qin->qoutlen = sizeof(struct qout64);
  167. memcpy (qin->qname, seg->dcss_name, 8);
  168. diag_cc = dcss_diag (DCSS_SEGEXT, qin, &dummy, &vmrc);
  169. if (diag_cc > 1) {
  170. PRINT_WARN ("segment_type: diag returned error %ld\n", vmrc);
  171. rc = dcss_diag_translate_rc (vmrc);
  172. goto out_free;
  173. }
  174. if (qout->segcnt > 6) {
  175. rc = -ENOTSUPP;
  176. goto out_free;
  177. }
  178. if (qout->segcnt == 1) {
  179. seg->vm_segtype = qout->range[0].start & 0xff;
  180. } else {
  181. /* multi-part segment. only one type supported here:
  182. - all parts are contiguous
  183. - all parts are either EW or EN type
  184. - maximum 6 parts allowed */
  185. unsigned long start = qout->segstart >> PAGE_SHIFT;
  186. for (i=0; i<qout->segcnt; i++) {
  187. if (((qout->range[i].start & 0xff) != SEG_TYPE_EW) &&
  188. ((qout->range[i].start & 0xff) != SEG_TYPE_EN)) {
  189. rc = -ENOTSUPP;
  190. goto out_free;
  191. }
  192. if (start != qout->range[i].start >> PAGE_SHIFT) {
  193. rc = -ENOTSUPP;
  194. goto out_free;
  195. }
  196. start = (qout->range[i].end >> PAGE_SHIFT) + 1;
  197. }
  198. seg->vm_segtype = SEG_TYPE_EWEN;
  199. }
  200. /* analyze diag output and update seg */
  201. seg->start_addr = qout->segstart;
  202. seg->end = qout->segend;
  203. memcpy (seg->range, qout->range, 6*sizeof(struct qrange));
  204. seg->segcnt = qout->segcnt;
  205. rc = 0;
  206. out_free:
  207. kfree(qin);
  208. kfree(qout);
  209. return rc;
  210. }
  211. /*
  212. * get info about a segment
  213. * possible return values:
  214. * -ENOSYS : we are not running on VM
  215. * -EIO : could not perform query diagnose
  216. * -ENOENT : no such segment
  217. * -ENOTSUPP: multi-part segment cannot be used with linux
  218. * -ENOSPC : segment cannot be used (overlaps with storage)
  219. * -ENOMEM : out of memory
  220. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  221. */
  222. int
  223. segment_type (char* name)
  224. {
  225. int rc;
  226. struct dcss_segment seg;
  227. if (!MACHINE_IS_VM)
  228. return -ENOSYS;
  229. dcss_mkname(name, seg.dcss_name);
  230. rc = query_segment_type (&seg);
  231. if (rc < 0)
  232. return rc;
  233. return seg.vm_segtype;
  234. }
  235. /*
  236. * real segment loading function, called from segment_load
  237. */
  238. static int
  239. __segment_load (char *name, int do_nonshared, unsigned long *addr, unsigned long *end)
  240. {
  241. struct dcss_segment *seg = kmalloc(sizeof(struct dcss_segment),
  242. GFP_DMA);
  243. int dcss_command, rc, diag_cc;
  244. if (seg == NULL) {
  245. rc = -ENOMEM;
  246. goto out;
  247. }
  248. dcss_mkname (name, seg->dcss_name);
  249. rc = query_segment_type (seg);
  250. if (rc < 0)
  251. goto out_free;
  252. rc = add_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
  253. switch (rc) {
  254. case 0:
  255. break;
  256. case -ENOSPC:
  257. PRINT_WARN("segment_load: not loading segment %s - overlaps "
  258. "storage/segment\n", name);
  259. goto out_free;
  260. case -ERANGE:
  261. PRINT_WARN("segment_load: not loading segment %s - exceeds "
  262. "kernel mapping range\n", name);
  263. goto out_free;
  264. default:
  265. PRINT_WARN("segment_load: not loading segment %s (rc: %d)\n",
  266. name, rc);
  267. goto out_free;
  268. }
  269. seg->res = kzalloc(sizeof(struct resource), GFP_KERNEL);
  270. if (seg->res == NULL) {
  271. rc = -ENOMEM;
  272. goto out_shared;
  273. }
  274. seg->res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
  275. seg->res->start = seg->start_addr;
  276. seg->res->end = seg->end;
  277. memcpy(&seg->res_name, seg->dcss_name, 8);
  278. EBCASC(seg->res_name, 8);
  279. seg->res_name[8] = '\0';
  280. strncat(seg->res_name, " (DCSS)", 7);
  281. seg->res->name = seg->res_name;
  282. rc = seg->vm_segtype;
  283. if (rc == SEG_TYPE_SC ||
  284. ((rc == SEG_TYPE_SR || rc == SEG_TYPE_ER) && !do_nonshared))
  285. seg->res->flags |= IORESOURCE_READONLY;
  286. if (request_resource(&iomem_resource, seg->res)) {
  287. rc = -EBUSY;
  288. kfree(seg->res);
  289. goto out_shared;
  290. }
  291. if (do_nonshared)
  292. dcss_command = DCSS_LOADNSR;
  293. else
  294. dcss_command = DCSS_LOADNOLY;
  295. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  296. &seg->start_addr, &seg->end);
  297. if (diag_cc > 1) {
  298. PRINT_WARN ("segment_load: could not load segment %s - "
  299. "diag returned error (%ld)\n",name,seg->end);
  300. rc = dcss_diag_translate_rc (seg->end);
  301. dcss_diag(DCSS_PURGESEG, seg->dcss_name,
  302. &seg->start_addr, &seg->end);
  303. goto out_resource;
  304. }
  305. seg->do_nonshared = do_nonshared;
  306. atomic_set(&seg->ref_count, 1);
  307. list_add(&seg->list, &dcss_list);
  308. *addr = seg->start_addr;
  309. *end = seg->end;
  310. if (do_nonshared)
  311. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  312. "type %s in non-shared mode\n", name,
  313. (void*)seg->start_addr, (void*)seg->end,
  314. segtype_string[seg->vm_segtype]);
  315. else {
  316. PRINT_INFO ("segment_load: loaded segment %s range %p .. %p "
  317. "type %s in shared mode\n", name,
  318. (void*)seg->start_addr, (void*)seg->end,
  319. segtype_string[seg->vm_segtype]);
  320. }
  321. goto out;
  322. out_resource:
  323. release_resource(seg->res);
  324. kfree(seg->res);
  325. out_shared:
  326. remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
  327. out_free:
  328. kfree(seg);
  329. out:
  330. return rc;
  331. }
  332. /*
  333. * this function loads a DCSS segment
  334. * name : name of the DCSS
  335. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  336. * 1 indicates that the dcss should be exclusive for this linux image
  337. * addr : will be filled with start address of the segment
  338. * end : will be filled with end address of the segment
  339. * return values:
  340. * -ENOSYS : we are not running on VM
  341. * -EIO : could not perform query or load diagnose
  342. * -ENOENT : no such segment
  343. * -ENOTSUPP: multi-part segment cannot be used with linux
  344. * -ENOSPC : segment cannot be used (overlaps with storage)
  345. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  346. * -ERANGE : segment cannot be used (exceeds kernel mapping range)
  347. * -EPERM : segment is currently loaded with incompatible permissions
  348. * -ENOMEM : out of memory
  349. * 0 .. 6 : type of segment as defined in include/asm-s390/extmem.h
  350. */
  351. int
  352. segment_load (char *name, int do_nonshared, unsigned long *addr,
  353. unsigned long *end)
  354. {
  355. struct dcss_segment *seg;
  356. int rc;
  357. if (!MACHINE_IS_VM)
  358. return -ENOSYS;
  359. mutex_lock(&dcss_lock);
  360. seg = segment_by_name (name);
  361. if (seg == NULL)
  362. rc = __segment_load (name, do_nonshared, addr, end);
  363. else {
  364. if (do_nonshared == seg->do_nonshared) {
  365. atomic_inc(&seg->ref_count);
  366. *addr = seg->start_addr;
  367. *end = seg->end;
  368. rc = seg->vm_segtype;
  369. } else {
  370. *addr = *end = 0;
  371. rc = -EPERM;
  372. }
  373. }
  374. mutex_unlock(&dcss_lock);
  375. return rc;
  376. }
  377. /*
  378. * this function modifies the shared state of a DCSS segment. note that
  379. * name : name of the DCSS
  380. * do_nonshared : 0 indicates that the dcss should be shared with other linux images
  381. * 1 indicates that the dcss should be exclusive for this linux image
  382. * return values:
  383. * -EIO : could not perform load diagnose (segment gone!)
  384. * -ENOENT : no such segment (segment gone!)
  385. * -EAGAIN : segment is in use by other exploiters, try later
  386. * -EINVAL : no segment with the given name is currently loaded - name invalid
  387. * -EBUSY : segment can temporarily not be used (overlaps with dcss)
  388. * 0 : operation succeeded
  389. */
  390. int
  391. segment_modify_shared (char *name, int do_nonshared)
  392. {
  393. struct dcss_segment *seg;
  394. unsigned long dummy;
  395. int dcss_command, rc, diag_cc;
  396. mutex_lock(&dcss_lock);
  397. seg = segment_by_name (name);
  398. if (seg == NULL) {
  399. rc = -EINVAL;
  400. goto out_unlock;
  401. }
  402. if (do_nonshared == seg->do_nonshared) {
  403. PRINT_INFO ("segment_modify_shared: not reloading segment %s"
  404. " - already in requested mode\n",name);
  405. rc = 0;
  406. goto out_unlock;
  407. }
  408. if (atomic_read (&seg->ref_count) != 1) {
  409. PRINT_WARN ("segment_modify_shared: not reloading segment %s - "
  410. "segment is in use by other driver(s)\n",name);
  411. rc = -EAGAIN;
  412. goto out_unlock;
  413. }
  414. release_resource(seg->res);
  415. if (do_nonshared) {
  416. dcss_command = DCSS_LOADNSR;
  417. seg->res->flags &= ~IORESOURCE_READONLY;
  418. } else {
  419. dcss_command = DCSS_LOADNOLY;
  420. if (seg->vm_segtype == SEG_TYPE_SR ||
  421. seg->vm_segtype == SEG_TYPE_ER)
  422. seg->res->flags |= IORESOURCE_READONLY;
  423. }
  424. if (request_resource(&iomem_resource, seg->res)) {
  425. PRINT_WARN("segment_modify_shared: could not reload segment %s"
  426. " - overlapping resources\n", name);
  427. rc = -EBUSY;
  428. kfree(seg->res);
  429. goto out_del;
  430. }
  431. dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
  432. diag_cc = dcss_diag(dcss_command, seg->dcss_name,
  433. &seg->start_addr, &seg->end);
  434. if (diag_cc > 1) {
  435. PRINT_WARN ("segment_modify_shared: could not reload segment %s"
  436. " - diag returned error (%ld)\n",name,seg->end);
  437. rc = dcss_diag_translate_rc (seg->end);
  438. goto out_del;
  439. }
  440. seg->do_nonshared = do_nonshared;
  441. rc = 0;
  442. goto out_unlock;
  443. out_del:
  444. remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
  445. list_del(&seg->list);
  446. dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
  447. kfree(seg);
  448. out_unlock:
  449. mutex_unlock(&dcss_lock);
  450. return rc;
  451. }
  452. /*
  453. * Decrease the use count of a DCSS segment and remove
  454. * it from the address space if nobody is using it
  455. * any longer.
  456. */
  457. void
  458. segment_unload(char *name)
  459. {
  460. unsigned long dummy;
  461. struct dcss_segment *seg;
  462. if (!MACHINE_IS_VM)
  463. return;
  464. mutex_lock(&dcss_lock);
  465. seg = segment_by_name (name);
  466. if (seg == NULL) {
  467. PRINT_ERR ("could not find segment %s in segment_unload, "
  468. "please report to linux390@de.ibm.com\n",name);
  469. goto out_unlock;
  470. }
  471. if (atomic_dec_return(&seg->ref_count) != 0)
  472. goto out_unlock;
  473. release_resource(seg->res);
  474. kfree(seg->res);
  475. remove_shared_memory(seg->start_addr, seg->end - seg->start_addr + 1);
  476. list_del(&seg->list);
  477. dcss_diag(DCSS_PURGESEG, seg->dcss_name, &dummy, &dummy);
  478. kfree(seg);
  479. out_unlock:
  480. mutex_unlock(&dcss_lock);
  481. }
  482. /*
  483. * save segment content permanently
  484. */
  485. void
  486. segment_save(char *name)
  487. {
  488. struct dcss_segment *seg;
  489. int startpfn = 0;
  490. int endpfn = 0;
  491. char cmd1[160];
  492. char cmd2[80];
  493. int i, response;
  494. if (!MACHINE_IS_VM)
  495. return;
  496. mutex_lock(&dcss_lock);
  497. seg = segment_by_name (name);
  498. if (seg == NULL) {
  499. PRINT_ERR("could not find segment %s in segment_save, please "
  500. "report to linux390@de.ibm.com\n", name);
  501. goto out;
  502. }
  503. startpfn = seg->start_addr >> PAGE_SHIFT;
  504. endpfn = (seg->end) >> PAGE_SHIFT;
  505. sprintf(cmd1, "DEFSEG %s", name);
  506. for (i=0; i<seg->segcnt; i++) {
  507. sprintf(cmd1+strlen(cmd1), " %X-%X %s",
  508. seg->range[i].start >> PAGE_SHIFT,
  509. seg->range[i].end >> PAGE_SHIFT,
  510. segtype_string[seg->range[i].start & 0xff]);
  511. }
  512. sprintf(cmd2, "SAVESEG %s", name);
  513. response = 0;
  514. cpcmd(cmd1, NULL, 0, &response);
  515. if (response) {
  516. PRINT_ERR("segment_save: DEFSEG failed with response code %i\n",
  517. response);
  518. goto out;
  519. }
  520. cpcmd(cmd2, NULL, 0, &response);
  521. if (response) {
  522. PRINT_ERR("segment_save: SAVESEG failed with response code %i\n",
  523. response);
  524. goto out;
  525. }
  526. out:
  527. mutex_unlock(&dcss_lock);
  528. }
  529. EXPORT_SYMBOL(segment_load);
  530. EXPORT_SYMBOL(segment_unload);
  531. EXPORT_SYMBOL(segment_save);
  532. EXPORT_SYMBOL(segment_type);
  533. EXPORT_SYMBOL(segment_modify_shared);