1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#define KMSG_COMPONENT "xpram"
30#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
31
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/ctype.h>
35#include <linux/errno.h>
36#include <linux/init.h>
37#include <linux/blkdev.h>
38#include <linux/blkpg.h>
39#include <linux/hdreg.h>
40#include <linux/device.h>
41#include <linux/bio.h>
42#include <linux/suspend.h>
43#include <linux/platform_device.h>
44#include <linux/gfp.h>
45#include <linux/uaccess.h>
46
47#define XPRAM_NAME "xpram"
48#define XPRAM_DEVS 1
49#define XPRAM_MAX_DEVS 32
50
51typedef struct {
52 unsigned int size;
53 unsigned int offset;
54} xpram_device_t;
55
56static xpram_device_t xpram_devices[XPRAM_MAX_DEVS];
57static unsigned int xpram_sizes[XPRAM_MAX_DEVS];
58static struct gendisk *xpram_disks[XPRAM_MAX_DEVS];
59static struct request_queue *xpram_queues[XPRAM_MAX_DEVS];
60static unsigned int xpram_pages;
61static int xpram_devs;
62
63
64
65
66static int devs = XPRAM_DEVS;
67static char *sizes[XPRAM_MAX_DEVS];
68
69module_param(devs, int, 0);
70module_param_array(sizes, charp, NULL, 0);
71
72MODULE_PARM_DESC(devs, "number of devices (\"partitions\"), " \
73 "the default is " __MODULE_STRING(XPRAM_DEVS) "\n");
74MODULE_PARM_DESC(sizes, "list of device (partition) sizes " \
75 "the defaults are 0s \n" \
76 "All devices with size 0 equally partition the "
77 "remaining space on the expanded strorage not "
78 "claimed by explicit sizes\n");
79MODULE_LICENSE("GPL");
80
81
82
83
84
85
86
87
88
89
90
91static int xpram_page_in (unsigned long page_addr, unsigned int xpage_index)
92{
93 int cc = 2;
94
95 asm volatile(
96 " .insn rre,0xb22e0000,%1,%2\n"
97 "0: ipm %0\n"
98 " srl %0,28\n"
99 "1:\n"
100 EX_TABLE(0b,1b)
101 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
102 if (cc == 3)
103 return -ENXIO;
104 if (cc == 2)
105 return -ENXIO;
106 if (cc == 1)
107 return -EIO;
108 return 0;
109}
110
111
112
113
114
115
116
117
118
119
120
121static long xpram_page_out (unsigned long page_addr, unsigned int xpage_index)
122{
123 int cc = 2;
124
125 asm volatile(
126 " .insn rre,0xb22f0000,%1,%2\n"
127 "0: ipm %0\n"
128 " srl %0,28\n"
129 "1:\n"
130 EX_TABLE(0b,1b)
131 : "+d" (cc) : "a" (__pa(page_addr)), "d" (xpage_index) : "cc");
132 if (cc == 3)
133 return -ENXIO;
134 if (cc == 2)
135 return -ENXIO;
136 if (cc == 1)
137 return -EIO;
138 return 0;
139}
140
141
142
143
144static int xpram_present(void)
145{
146 unsigned long mem_page;
147 int rc;
148
149 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
150 if (!mem_page)
151 return -ENOMEM;
152 rc = xpram_page_in(mem_page, 0);
153 free_page(mem_page);
154 return rc ? -ENXIO : 0;
155}
156
157
158
159
160static unsigned long xpram_highest_page_index(void)
161{
162 unsigned int page_index, add_bit;
163 unsigned long mem_page;
164
165 mem_page = (unsigned long) __get_free_page(GFP_KERNEL);
166 if (!mem_page)
167 return 0;
168
169 page_index = 0;
170 add_bit = 1ULL << (sizeof(unsigned int)*8 - 1);
171 while (add_bit > 0) {
172 if (xpram_page_in(mem_page, page_index | add_bit) == 0)
173 page_index |= add_bit;
174 add_bit >>= 1;
175 }
176
177 free_page (mem_page);
178
179 return page_index;
180}
181
182
183
184
185static blk_qc_t xpram_make_request(struct request_queue *q, struct bio *bio)
186{
187 xpram_device_t *xdev = bio->bi_disk->private_data;
188 struct bio_vec bvec;
189 struct bvec_iter iter;
190 unsigned int index;
191 unsigned long page_addr;
192 unsigned long bytes;
193
194 blk_queue_split(q, &bio);
195
196 if ((bio->bi_iter.bi_sector & 7) != 0 ||
197 (bio->bi_iter.bi_size & 4095) != 0)
198
199 goto fail;
200 if ((bio->bi_iter.bi_size >> 12) > xdev->size)
201
202 goto fail;
203 if ((bio->bi_iter.bi_sector >> 3) > 0xffffffffU - xdev->offset)
204 goto fail;
205 index = (bio->bi_iter.bi_sector >> 3) + xdev->offset;
206 bio_for_each_segment(bvec, bio, iter) {
207 page_addr = (unsigned long)
208 kmap(bvec.bv_page) + bvec.bv_offset;
209 bytes = bvec.bv_len;
210 if ((page_addr & 4095) != 0 || (bytes & 4095) != 0)
211
212 goto fail;
213 while (bytes > 0) {
214 if (bio_data_dir(bio) == READ) {
215 if (xpram_page_in(page_addr, index) != 0)
216 goto fail;
217 } else {
218 if (xpram_page_out(page_addr, index) != 0)
219 goto fail;
220 }
221 page_addr += 4096;
222 bytes -= 4096;
223 index++;
224 }
225 }
226 bio_endio(bio);
227 return BLK_QC_T_NONE;
228fail:
229 bio_io_error(bio);
230 return BLK_QC_T_NONE;
231}
232
233static int xpram_getgeo(struct block_device *bdev, struct hd_geometry *geo)
234{
235 unsigned long size;
236
237
238
239
240
241
242 size = (xpram_pages * 8) & ~0x3f;
243 geo->cylinders = size >> 6;
244 geo->heads = 4;
245 geo->sectors = 16;
246 geo->start = 4;
247 return 0;
248}
249
250static const struct block_device_operations xpram_devops =
251{
252 .owner = THIS_MODULE,
253 .getgeo = xpram_getgeo,
254};
255
256
257
258
259static int __init xpram_setup_sizes(unsigned long pages)
260{
261 unsigned long mem_needed;
262 unsigned long mem_auto;
263 unsigned long long size;
264 char *sizes_end;
265 int mem_auto_no;
266 int i;
267
268
269 if (devs <= 0 || devs > XPRAM_MAX_DEVS) {
270 pr_err("%d is not a valid number of XPRAM devices\n",devs);
271 return -EINVAL;
272 }
273 xpram_devs = devs;
274
275
276
277
278
279 mem_needed = 0;
280 mem_auto_no = 0;
281 for (i = 0; i < xpram_devs; i++) {
282 if (sizes[i]) {
283 size = simple_strtoull(sizes[i], &sizes_end, 0);
284 switch (*sizes_end) {
285 case 'g':
286 case 'G':
287 size <<= 20;
288 break;
289 case 'm':
290 case 'M':
291 size <<= 10;
292 }
293 xpram_sizes[i] = (size + 3) & -4UL;
294 }
295 if (xpram_sizes[i])
296 mem_needed += xpram_sizes[i];
297 else
298 mem_auto_no++;
299 }
300
301 pr_info(" number of devices (partitions): %d \n", xpram_devs);
302 for (i = 0; i < xpram_devs; i++) {
303 if (xpram_sizes[i])
304 pr_info(" size of partition %d: %u kB\n",
305 i, xpram_sizes[i]);
306 else
307 pr_info(" size of partition %d to be set "
308 "automatically\n",i);
309 }
310 pr_info(" memory needed (for sized partitions): %lu kB\n",
311 mem_needed);
312 pr_info(" partitions to be sized automatically: %d\n",
313 mem_auto_no);
314
315 if (mem_needed > pages * 4) {
316 pr_err("Not enough expanded memory available\n");
317 return -EINVAL;
318 }
319
320
321
322
323
324
325
326 if (mem_auto_no) {
327 mem_auto = ((pages - mem_needed / 4) / mem_auto_no) * 4;
328 pr_info(" automatically determined "
329 "partition size: %lu kB\n", mem_auto);
330 for (i = 0; i < xpram_devs; i++)
331 if (xpram_sizes[i] == 0)
332 xpram_sizes[i] = mem_auto;
333 }
334 return 0;
335}
336
337static int __init xpram_setup_blkdev(void)
338{
339 unsigned long offset;
340 int i, rc = -ENOMEM;
341
342 for (i = 0; i < xpram_devs; i++) {
343 xpram_disks[i] = alloc_disk(1);
344 if (!xpram_disks[i])
345 goto out;
346 xpram_queues[i] = blk_alloc_queue(GFP_KERNEL);
347 if (!xpram_queues[i]) {
348 put_disk(xpram_disks[i]);
349 goto out;
350 }
351 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, xpram_queues[i]);
352 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, xpram_queues[i]);
353 blk_queue_make_request(xpram_queues[i], xpram_make_request);
354 blk_queue_logical_block_size(xpram_queues[i], 4096);
355 }
356
357
358
359
360 rc = register_blkdev(XPRAM_MAJOR, XPRAM_NAME);
361 if (rc < 0)
362 goto out;
363
364
365
366
367 offset = 0;
368 for (i = 0; i < xpram_devs; i++) {
369 struct gendisk *disk = xpram_disks[i];
370
371 xpram_devices[i].size = xpram_sizes[i] / 4;
372 xpram_devices[i].offset = offset;
373 offset += xpram_devices[i].size;
374 disk->major = XPRAM_MAJOR;
375 disk->first_minor = i;
376 disk->fops = &xpram_devops;
377 disk->private_data = &xpram_devices[i];
378 disk->queue = xpram_queues[i];
379 sprintf(disk->disk_name, "slram%d", i);
380 set_capacity(disk, xpram_sizes[i] << 1);
381 add_disk(disk);
382 }
383
384 return 0;
385out:
386 while (i--) {
387 blk_cleanup_queue(xpram_queues[i]);
388 put_disk(xpram_disks[i]);
389 }
390 return rc;
391}
392
393
394
395
396static void xpram_resume_error(const char *message)
397{
398 pr_err("Resuming the system failed: %s\n", message);
399 panic("xpram resume error\n");
400}
401
402
403
404
405static int xpram_restore(struct device *dev)
406{
407 if (!xpram_pages)
408 return 0;
409 if (xpram_present() != 0)
410 xpram_resume_error("xpram disappeared");
411 if (xpram_pages != xpram_highest_page_index() + 1)
412 xpram_resume_error("Size of xpram changed");
413 return 0;
414}
415
416static const struct dev_pm_ops xpram_pm_ops = {
417 .restore = xpram_restore,
418};
419
420static struct platform_driver xpram_pdrv = {
421 .driver = {
422 .name = XPRAM_NAME,
423 .pm = &xpram_pm_ops,
424 },
425};
426
427static struct platform_device *xpram_pdev;
428
429
430
431
432static void __exit xpram_exit(void)
433{
434 int i;
435 for (i = 0; i < xpram_devs; i++) {
436 del_gendisk(xpram_disks[i]);
437 blk_cleanup_queue(xpram_queues[i]);
438 put_disk(xpram_disks[i]);
439 }
440 unregister_blkdev(XPRAM_MAJOR, XPRAM_NAME);
441 platform_device_unregister(xpram_pdev);
442 platform_driver_unregister(&xpram_pdrv);
443}
444
445static int __init xpram_init(void)
446{
447 int rc;
448
449
450 if (xpram_present() != 0) {
451 pr_err("No expanded memory available\n");
452 return -ENODEV;
453 }
454 xpram_pages = xpram_highest_page_index() + 1;
455 pr_info(" %u pages expanded memory found (%lu KB).\n",
456 xpram_pages, (unsigned long) xpram_pages*4);
457 rc = xpram_setup_sizes(xpram_pages);
458 if (rc)
459 return rc;
460 rc = platform_driver_register(&xpram_pdrv);
461 if (rc)
462 return rc;
463 xpram_pdev = platform_device_register_simple(XPRAM_NAME, -1, NULL, 0);
464 if (IS_ERR(xpram_pdev)) {
465 rc = PTR_ERR(xpram_pdev);
466 goto fail_platform_driver_unregister;
467 }
468 rc = xpram_setup_blkdev();
469 if (rc)
470 goto fail_platform_device_unregister;
471 return 0;
472
473fail_platform_device_unregister:
474 platform_device_unregister(xpram_pdev);
475fail_platform_driver_unregister:
476 platform_driver_unregister(&xpram_pdrv);
477 return rc;
478}
479
480module_init(xpram_init);
481module_exit(xpram_exit);
482