virtio_khv_mmio.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. #define pr_fmt(fmt) "virtio-khv-mmio: " fmt
  3. #include <linux/acpi.h>
  4. #include <linux/dma-mapping.h>
  5. #include <linux/highmem.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/io.h>
  8. #include <linux/list.h>
  9. #include <linux/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/virtio.h>
  14. #include <linux/virtio_config.h>
  15. #include <uapi/linux/virtio_mmio.h>
  16. #include <linux/mmio_khv.h>
  17. #include <linux/virtio_ring.h>
  18. #include <linux/virtio_ids.h>
  19. static volatile unsigned char __iomem *notify_guest_reg;
  20. static volatile unsigned char __iomem *notify_host_reg;
  21. /* The alignment to use between consumer and producer parts of vring.
  22. * Currently hardcoded to the page size. */
  23. #define VIRTIO_MMIO_VRING_ALIGN PAGE_SIZE
  24. #define to_virtio_khv_mmio_device(_plat_dev) \
  25. container_of(_plat_dev, struct virtio_khv_mmio_device, vdev)
  26. struct virtio_khv_mmio_device {
  27. struct virtio_device vdev;
  28. struct platform_device *pdev;
  29. u64 base;
  30. unsigned long version;
  31. /* a list of queues so we can dispatch IRQs */
  32. spinlock_t lock;
  33. struct list_head virtqueues;
  34. };
  35. struct virtio_khv_mmio_vq_info {
  36. /* the actual virtqueue */
  37. struct virtqueue *vq;
  38. /* the list node for the virtqueues list */
  39. struct list_head node;
  40. };
  41. /* Configuration interface */
  42. static u64 vm_get_features(struct virtio_device *vdev)
  43. {
  44. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  45. u64 features;
  46. writex(1, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL, WIDTH_32);
  47. features = readx(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES, WIDTH_32);
  48. features <<= 32;
  49. writex(0, vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES_SEL, WIDTH_32);
  50. features |= readx(vm_dev->base + VIRTIO_MMIO_DEVICE_FEATURES, WIDTH_32);
  51. return features;
  52. }
  53. static int vm_finalize_features(struct virtio_device *vdev)
  54. {
  55. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  56. /* Give virtio_ring a chance to accept features. */
  57. vring_transport_features(vdev);
  58. /* Make sure there is are no mixed devices */
  59. if (vm_dev->version == 2 &&
  60. !__virtio_test_bit(vdev, VIRTIO_F_VERSION_1)) {
  61. dev_err(&vdev->dev, "New virtio-mmio devices (version 2) must provide VIRTIO_F_VERSION_1 feature!\n");
  62. return -EINVAL;
  63. }
  64. writex(1, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL, WIDTH_32);
  65. writex((u32)(vdev->features >> 32),
  66. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES, WIDTH_32);
  67. writex(0, vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES_SEL, WIDTH_32);
  68. writex((u32)vdev->features,
  69. vm_dev->base + VIRTIO_MMIO_DRIVER_FEATURES, WIDTH_32);
  70. return 0;
  71. }
  72. static void vm_get(struct virtio_device *vdev, unsigned offset,
  73. void *buf, unsigned len)
  74. {
  75. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  76. u64 base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  77. u8 b;
  78. __le16 w;
  79. __le32 l;
  80. if (vm_dev->version == 1) {
  81. u8 *ptr = buf;
  82. int i;
  83. for (i = 0; i < len; i++)
  84. ptr[i] = readx(base + offset + i, WIDTH_8);
  85. return;
  86. }
  87. switch (len) {
  88. case 1:
  89. b = readx(base + offset, WIDTH_8);
  90. memcpy(buf, &b, sizeof b);
  91. break;
  92. case 2:
  93. w = cpu_to_le16(readx(base + offset, WIDTH_16));
  94. memcpy(buf, &w, sizeof w);
  95. break;
  96. case 4:
  97. l = cpu_to_le32(readx(base + offset, WIDTH_32));
  98. memcpy(buf, &l, sizeof l);
  99. break;
  100. case 8:
  101. l = cpu_to_le32(readx(base + offset, WIDTH_32));
  102. memcpy(buf, &l, sizeof l);
  103. l = cpu_to_le32(readx(base + offset + sizeof l, WIDTH_32));
  104. memcpy(buf + sizeof l, &l, sizeof l);
  105. break;
  106. default:
  107. BUG();
  108. }
  109. }
  110. static void vm_set(struct virtio_device *vdev, unsigned offset,
  111. const void *buf, unsigned len)
  112. {
  113. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  114. u64 base = vm_dev->base + VIRTIO_MMIO_CONFIG;
  115. u8 b;
  116. __le16 w;
  117. __le32 l;
  118. if (vm_dev->version == 1) {
  119. const u8 *ptr = buf;
  120. int i;
  121. for (i = 0; i < len; i++)
  122. writex(ptr[i], base + offset + i, WIDTH_8);
  123. return;
  124. }
  125. switch (len) {
  126. case 1:
  127. memcpy(&b, buf, sizeof b);
  128. writex(b, base + offset, WIDTH_8);
  129. break;
  130. case 2:
  131. memcpy(&w, buf, sizeof w);
  132. writex(le16_to_cpu(w), base + offset, WIDTH_16);
  133. break;
  134. case 4:
  135. memcpy(&l, buf, sizeof l);
  136. writex(le32_to_cpu(l), base + offset, WIDTH_32);
  137. break;
  138. case 8:
  139. memcpy(&l, buf, sizeof l);
  140. writex(le32_to_cpu(l), base + offset, WIDTH_32);
  141. memcpy(&l, buf + sizeof l, sizeof l);
  142. writex(le32_to_cpu(l), base + offset + sizeof l, WIDTH_32);
  143. break;
  144. default:
  145. BUG();
  146. }
  147. }
  148. static u32 vm_generation(struct virtio_device *vdev)
  149. {
  150. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  151. if (vm_dev->version == 1)
  152. return 0;
  153. else
  154. return readx(vm_dev->base + VIRTIO_MMIO_CONFIG_GENERATION, WIDTH_32);
  155. }
  156. static u8 vm_get_status(struct virtio_device *vdev)
  157. {
  158. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  159. return readx(vm_dev->base + VIRTIO_MMIO_STATUS, WIDTH_32) & 0xff;
  160. }
  161. static void vm_set_status(struct virtio_device *vdev, u8 status)
  162. {
  163. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  164. /* We should never be setting status to 0. */
  165. BUG_ON(status == 0);
  166. writex(status, vm_dev->base + VIRTIO_MMIO_STATUS, WIDTH_32);
  167. }
  168. static void vm_reset(struct virtio_device *vdev)
  169. {
  170. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  171. /* 0 status means a reset. */
  172. writex(0, vm_dev->base + VIRTIO_MMIO_STATUS, WIDTH_32);
  173. }
  174. /* Transport interface */
  175. /* the notify function used when creating a virt queue */
  176. static bool vm_notify(struct virtqueue *vq)
  177. {
  178. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vq->vdev);
  179. writex(vq->index, vm_dev->base + VIRTIO_MMIO_QUEUE_NOTIFY, WIDTH_32);
  180. return true;
  181. }
  182. /* Notify all virtqueues on an interrupt. */
  183. static irqreturn_t vm_interrupt(int irq, void *opaque)
  184. {
  185. struct virtio_khv_mmio_device *vm_dev = opaque;
  186. struct virtio_khv_mmio_vq_info *info;
  187. unsigned long flags;
  188. irqreturn_t ret = IRQ_NONE;
  189. #ifdef CONFIG_SOC_INT_SRC7
  190. writel(0, notify_guest_reg);
  191. #else
  192. /* clear interrupt */
  193. writel(1, notify_guest_reg + 0x4);
  194. #endif
  195. spin_lock_irqsave(&vm_dev->lock, flags);
  196. list_for_each_entry(info, &vm_dev->virtqueues, node)
  197. ret |= vring_interrupt(irq, info->vq);
  198. spin_unlock_irqrestore(&vm_dev->lock, flags);
  199. return ret;
  200. }
  201. static void vm_del_vq(struct virtqueue *vq)
  202. {
  203. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vq->vdev);
  204. struct virtio_khv_mmio_vq_info *info = vq->priv;
  205. unsigned long flags;
  206. unsigned int index = vq->index;
  207. spin_lock_irqsave(&vm_dev->lock, flags);
  208. list_del(&info->node);
  209. spin_unlock_irqrestore(&vm_dev->lock, flags);
  210. /* Select and deactivate the queue */
  211. writex(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL, WIDTH_32);
  212. if (vm_dev->version == 1) {
  213. writex(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN, WIDTH_32);
  214. } else {
  215. writex(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY, WIDTH_32);
  216. WARN_ON(readx(vm_dev->base + VIRTIO_MMIO_QUEUE_READY, WIDTH_32));
  217. }
  218. vring_del_virtqueue(vq);
  219. kfree(info);
  220. }
  221. static void vm_del_vqs(struct virtio_device *vdev)
  222. {
  223. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  224. struct virtqueue *vq, *n;
  225. list_for_each_entry_safe(vq, n, &vdev->vqs, list)
  226. vm_del_vq(vq);
  227. free_irq(platform_get_irq(vm_dev->pdev, 0), vm_dev);
  228. }
  229. static struct virtqueue *vm_setup_vq(struct virtio_device *vdev, unsigned index,
  230. void (*callback)(struct virtqueue *vq),
  231. const char *name, bool ctx)
  232. {
  233. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  234. struct virtio_khv_mmio_vq_info *info;
  235. struct virtqueue *vq;
  236. unsigned long flags;
  237. unsigned int num;
  238. int err;
  239. if (!name)
  240. return NULL;
  241. /* Select the queue we're interested in */
  242. writex(index, vm_dev->base + VIRTIO_MMIO_QUEUE_SEL, WIDTH_32);
  243. /* Queue shouldn't already be set up. */
  244. if (readx(vm_dev->base + (vm_dev->version == 1 ?
  245. VIRTIO_MMIO_QUEUE_PFN : VIRTIO_MMIO_QUEUE_READY), WIDTH_32)) {
  246. err = -ENOENT;
  247. goto error_available;
  248. }
  249. /* Allocate and fill out our active queue description */
  250. info = kmalloc(sizeof(*info), GFP_KERNEL);
  251. if (!info) {
  252. err = -ENOMEM;
  253. goto error_kmalloc;
  254. }
  255. num = readx(vm_dev->base + VIRTIO_MMIO_QUEUE_NUM_MAX, WIDTH_32);
  256. if (num == 0) {
  257. err = -ENOENT;
  258. goto error_new_virtqueue;
  259. }
  260. /* Create the vring */
  261. vq = vring_create_virtqueue(index, num, VIRTIO_MMIO_VRING_ALIGN, vdev,
  262. true, true, ctx, vm_notify, callback, name);
  263. if (!vq) {
  264. err = -ENOMEM;
  265. goto error_new_virtqueue;
  266. }
  267. /* Activate the queue */
  268. writex(virtqueue_get_vring_size(vq), vm_dev->base + VIRTIO_MMIO_QUEUE_NUM, WIDTH_32);
  269. if (vm_dev->version == 1) {
  270. u64 q_pfn = virtqueue_get_desc_addr(vq) >> PAGE_SHIFT;
  271. /*
  272. * virtio-mmio v1 uses a 32bit QUEUE PFN. If we have something
  273. * that doesn't fit in 32bit, fail the setup rather than
  274. * pretending to be successful.
  275. */
  276. if (q_pfn >> 32) {
  277. dev_err(&vdev->dev,
  278. "platform bug: legacy virtio-mmio must not be used with RAM above 0x%llxGB\n",
  279. 0x1ULL << (32 + PAGE_SHIFT - 30));
  280. err = -E2BIG;
  281. goto error_bad_pfn;
  282. }
  283. writex(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_QUEUE_ALIGN, WIDTH_32);
  284. writex(q_pfn, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN, WIDTH_32);
  285. } else {
  286. u64 addr;
  287. addr = virtqueue_get_desc_addr(vq);
  288. writex((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_LOW, WIDTH_32);
  289. writex((u32)(addr >> 32),
  290. vm_dev->base + VIRTIO_MMIO_QUEUE_DESC_HIGH, WIDTH_32);
  291. addr = virtqueue_get_avail_addr(vq);
  292. writex((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_LOW, WIDTH_32);
  293. writex((u32)(addr >> 32),
  294. vm_dev->base + VIRTIO_MMIO_QUEUE_AVAIL_HIGH, WIDTH_32);
  295. addr = virtqueue_get_used_addr(vq);
  296. writex((u32)addr, vm_dev->base + VIRTIO_MMIO_QUEUE_USED_LOW, WIDTH_32);
  297. writex((u32)(addr >> 32),
  298. vm_dev->base + VIRTIO_MMIO_QUEUE_USED_HIGH, WIDTH_32);
  299. writex(1, vm_dev->base + VIRTIO_MMIO_QUEUE_READY, WIDTH_32);
  300. }
  301. vq->priv = info;
  302. info->vq = vq;
  303. spin_lock_irqsave(&vm_dev->lock, flags);
  304. list_add(&info->node, &vm_dev->virtqueues);
  305. spin_unlock_irqrestore(&vm_dev->lock, flags);
  306. return vq;
  307. error_bad_pfn:
  308. vring_del_virtqueue(vq);
  309. error_new_virtqueue:
  310. if (vm_dev->version == 1) {
  311. writex(0, vm_dev->base + VIRTIO_MMIO_QUEUE_PFN, WIDTH_32);
  312. } else {
  313. writex(0, vm_dev->base + VIRTIO_MMIO_QUEUE_READY, WIDTH_32);
  314. WARN_ON(readx(vm_dev->base + VIRTIO_MMIO_QUEUE_READY, WIDTH_32));
  315. }
  316. kfree(info);
  317. error_kmalloc:
  318. error_available:
  319. return ERR_PTR(err);
  320. }
  321. static int vm_find_vqs(struct virtio_device *vdev, unsigned nvqs,
  322. struct virtqueue *vqs[],
  323. vq_callback_t *callbacks[],
  324. const char * const names[],
  325. const bool *ctx,
  326. struct irq_affinity *desc)
  327. {
  328. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  329. int irq = platform_get_irq(vm_dev->pdev, 0);
  330. int i, err, queue_idx = 0;
  331. if (irq < 0)
  332. return irq;
  333. err = request_irq(irq, vm_interrupt, IRQF_SHARED,
  334. dev_name(&vdev->dev), vm_dev);
  335. if (err)
  336. return err;
  337. for (i = 0; i < nvqs; ++i) {
  338. if (!names[i]) {
  339. vqs[i] = NULL;
  340. continue;
  341. }
  342. vqs[i] = vm_setup_vq(vdev, queue_idx++, callbacks[i], names[i],
  343. ctx ? ctx[i] : false);
  344. if (IS_ERR(vqs[i])) {
  345. vm_del_vqs(vdev);
  346. return PTR_ERR(vqs[i]);
  347. }
  348. }
  349. return 0;
  350. }
  351. static const char *vm_bus_name(struct virtio_device *vdev)
  352. {
  353. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  354. return vm_dev->pdev->name;
  355. }
  356. static bool vm_get_shm_region(struct virtio_device *vdev,
  357. struct virtio_shm_region *region, u8 id)
  358. {
  359. struct virtio_khv_mmio_device *vm_dev = to_virtio_khv_mmio_device(vdev);
  360. u64 len, addr;
  361. /* Select the region we're interested in */
  362. writex(id, vm_dev->base + VIRTIO_MMIO_SHM_SEL, WIDTH_32);
  363. /* Read the region size */
  364. len = (u64) readx(vm_dev->base + VIRTIO_MMIO_SHM_LEN_LOW, WIDTH_32);
  365. len |= (u64) readx(vm_dev->base + VIRTIO_MMIO_SHM_LEN_HIGH, WIDTH_32) << 32;
  366. region->len = len;
  367. /* Check if region length is -1. If that's the case, the shared memory
  368. * region does not exist and there is no need to proceed further.
  369. */
  370. if (len == ~(u64)0)
  371. return false;
  372. /* Read the region base address */
  373. addr = (u64) readx(vm_dev->base + VIRTIO_MMIO_SHM_BASE_LOW, WIDTH_32);
  374. addr |= (u64) readx(vm_dev->base + VIRTIO_MMIO_SHM_BASE_HIGH, WIDTH_32) << 32;
  375. region->addr = addr;
  376. return true;
  377. }
  378. static const struct virtio_config_ops virtio_khv_mmio_config_ops = {
  379. .get = vm_get,
  380. .set = vm_set,
  381. .generation = vm_generation,
  382. .get_status = vm_get_status,
  383. .set_status = vm_set_status,
  384. .reset = vm_reset,
  385. .find_vqs = vm_find_vqs,
  386. .del_vqs = vm_del_vqs,
  387. .get_features = vm_get_features,
  388. .finalize_features = vm_finalize_features,
  389. .bus_name = vm_bus_name,
  390. .get_shm_region = vm_get_shm_region,
  391. };
  392. static void virtio_khv_mmio_release_dev(struct device *_d)
  393. {
  394. struct virtio_device *vdev =
  395. container_of(_d, struct virtio_device, dev);
  396. struct virtio_khv_mmio_device *vm_dev =
  397. container_of(vdev, struct virtio_khv_mmio_device, vdev);
  398. struct platform_device *pdev = vm_dev->pdev;
  399. devm_kfree(&pdev->dev, vm_dev);
  400. }
  401. /* Platform device */
  402. static int virtio_khv_mmio_probe(struct platform_device *pdev)
  403. {
  404. struct virtio_khv_mmio_device *vm_dev;
  405. unsigned long magic;
  406. int rc;
  407. vm_dev = devm_kzalloc(&pdev->dev, sizeof(*vm_dev), GFP_KERNEL);
  408. if (!vm_dev)
  409. return -ENOMEM;
  410. vm_dev->vdev.dev.parent = &pdev->dev;
  411. vm_dev->vdev.dev.release = virtio_khv_mmio_release_dev;
  412. vm_dev->vdev.config = &virtio_khv_mmio_config_ops;
  413. vm_dev->pdev = pdev;
  414. INIT_LIST_HEAD(&vm_dev->virtqueues);
  415. spin_lock_init(&vm_dev->lock);
  416. #ifdef CONFIG_SOC_INT_SRC7
  417. notify_guest_reg = ioremap(0xFFEF018094, 4);
  418. notify_host_reg = ioremap(0xFFFF019094, 4);
  419. #else
  420. notify_guest_reg = ioremap(0xffefc50000, 0x100);
  421. notify_host_reg = ioremap(0xffffc3b000, 0x100);
  422. writel(1, notify_host_reg + 0xc);
  423. writel(1, notify_host_reg + 0x4);
  424. #endif
  425. rc = of_property_read_u64((&pdev->dev)->of_node, "reg", &vm_dev->base);
  426. if (rc < 0)
  427. return -EFAULT;
  428. /* Check magic value */
  429. magic = readx(vm_dev->base + VIRTIO_MMIO_MAGIC_VALUE, WIDTH_32);
  430. if (magic != ('v' | 'i' << 8 | 'r' << 16 | 't' << 24)) {
  431. dev_warn(&pdev->dev, "Wrong magic value 0x%08lx!\n", magic);
  432. return -ENODEV;
  433. }
  434. /* Check device version */
  435. vm_dev->version = readx(vm_dev->base + VIRTIO_MMIO_VERSION, WIDTH_32);
  436. if (vm_dev->version < 1 || vm_dev->version > 2) {
  437. dev_err(&pdev->dev, "Version %ld not supported!\n",
  438. vm_dev->version);
  439. return -ENXIO;
  440. }
  441. vm_dev->vdev.id.device = readx(vm_dev->base + VIRTIO_MMIO_DEVICE_ID, WIDTH_32);
  442. if (vm_dev->vdev.id.device == 0) {
  443. /*
  444. * virtio-mmio device with an ID 0 is a (dummy) placeholder
  445. * with no function. End probing now with no error reported.
  446. */
  447. return -ENODEV;
  448. }
  449. vm_dev->vdev.id.vendor = readx(vm_dev->base + VIRTIO_MMIO_VENDOR_ID, WIDTH_32);
  450. if (vm_dev->version == 1) {
  451. writex(PAGE_SIZE, vm_dev->base + VIRTIO_MMIO_GUEST_PAGE_SIZE, WIDTH_32);
  452. rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  453. /*
  454. * In the legacy case, ensure our coherently-allocated virtio
  455. * ring will be at an address expressable as a 32-bit PFN.
  456. */
  457. if (!rc)
  458. dma_set_coherent_mask(&pdev->dev,
  459. DMA_BIT_MASK(32 + PAGE_SHIFT));
  460. } else {
  461. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  462. }
  463. if (rc)
  464. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  465. if (rc)
  466. dev_warn(&pdev->dev, "Failed to enable 64-bit or 32-bit DMA. Trying to continue, but this might not work.\n");
  467. platform_set_drvdata(pdev, vm_dev);
  468. rc = register_virtio_device(&vm_dev->vdev);
  469. if (rc)
  470. put_device(&vm_dev->vdev.dev);
  471. return rc;
  472. }
  473. static int virtio_khv_mmio_remove(struct platform_device *pdev)
  474. {
  475. struct virtio_khv_mmio_device *vm_dev = platform_get_drvdata(pdev);
  476. unregister_virtio_device(&vm_dev->vdev);
  477. return 0;
  478. }
  479. /* Devices list parameter */
  480. #if defined(CONFIG_VIRTIO_MMIO_CMDLINE_DEVICES)
  481. static struct device vm_cmdline_parent = {
  482. .init_name = "virtio-mmio-cmdline",
  483. };
  484. static int vm_cmdline_parent_registered;
  485. static int vm_cmdline_id;
  486. static int vm_cmdline_set(const char *device,
  487. const struct kernel_param *kp)
  488. {
  489. int err;
  490. struct resource resources[2] = {};
  491. char *str;
  492. long long int base, size;
  493. unsigned int irq;
  494. int processed, consumed = 0;
  495. struct platform_device *pdev;
  496. /* Consume "size" part of the command line parameter */
  497. size = memparse(device, &str);
  498. /* Get "@<base>:<irq>[:<id>]" chunks */
  499. processed = sscanf(str, "@%lli:%u%n:%d%n",
  500. &base, &irq, &consumed,
  501. &vm_cmdline_id, &consumed);
  502. /*
  503. * sscanf() must process at least 2 chunks; also there
  504. * must be no extra characters after the last chunk, so
  505. * str[consumed] must be '\0'
  506. */
  507. if (processed < 2 || str[consumed] || irq == 0)
  508. return -EINVAL;
  509. resources[0].flags = IORESOURCE_MEM;
  510. resources[0].start = base;
  511. resources[0].end = base + size - 1;
  512. resources[1].flags = IORESOURCE_IRQ;
  513. resources[1].start = resources[1].end = irq;
  514. if (!vm_cmdline_parent_registered) {
  515. err = device_register(&vm_cmdline_parent);
  516. if (err) {
  517. pr_err("Failed to register parent device!\n");
  518. return err;
  519. }
  520. vm_cmdline_parent_registered = 1;
  521. }
  522. pr_info("Registering device virtio-mmio.%d at 0x%llx-0x%llx, IRQ %d.\n",
  523. vm_cmdline_id,
  524. (unsigned long long)resources[0].start,
  525. (unsigned long long)resources[0].end,
  526. (int)resources[1].start);
  527. pdev = platform_device_register_resndata(&vm_cmdline_parent,
  528. "virtio-mmio", vm_cmdline_id++,
  529. resources, ARRAY_SIZE(resources), NULL, 0);
  530. return PTR_ERR_OR_ZERO(pdev);
  531. }
  532. static int vm_cmdline_get_device(struct device *dev, void *data)
  533. {
  534. char *buffer = data;
  535. unsigned int len = strlen(buffer);
  536. struct platform_device *pdev = to_platform_device(dev);
  537. snprintf(buffer + len, PAGE_SIZE - len, "0x%llx@0x%llx:%llu:%d\n",
  538. pdev->resource[0].end - pdev->resource[0].start + 1ULL,
  539. (unsigned long long)pdev->resource[0].start,
  540. (unsigned long long)pdev->resource[1].start,
  541. pdev->id);
  542. return 0;
  543. }
  544. static int vm_cmdline_get(char *buffer, const struct kernel_param *kp)
  545. {
  546. buffer[0] = '\0';
  547. device_for_each_child(&vm_cmdline_parent, buffer,
  548. vm_cmdline_get_device);
  549. return strlen(buffer) + 1;
  550. }
  551. static const struct kernel_param_ops vm_cmdline_param_ops = {
  552. .set = vm_cmdline_set,
  553. .get = vm_cmdline_get,
  554. };
  555. device_param_cb(device, &vm_cmdline_param_ops, NULL, S_IRUSR);
  556. static int vm_unregister_cmdline_device(struct device *dev,
  557. void *data)
  558. {
  559. platform_device_unregister(to_platform_device(dev));
  560. return 0;
  561. }
  562. static void vm_unregister_cmdline_devices(void)
  563. {
  564. if (vm_cmdline_parent_registered) {
  565. device_for_each_child(&vm_cmdline_parent, NULL,
  566. vm_unregister_cmdline_device);
  567. device_unregister(&vm_cmdline_parent);
  568. vm_cmdline_parent_registered = 0;
  569. }
  570. }
  571. #else
  572. static void vm_unregister_cmdline_devices(void)
  573. {
  574. }
  575. #endif
  576. /* Platform driver */
  577. static const struct of_device_id virtio_khv_mmio_match[] = {
  578. { .compatible = "virtio,khv-mmio", },
  579. {},
  580. };
  581. MODULE_DEVICE_TABLE(of, virtio_khv_mmio_match);
  582. #ifdef CONFIG_ACPI
  583. static const struct acpi_device_id virtio_khv_mmio_acpi_match[] = {
  584. { "LNRO0005", },
  585. { }
  586. };
  587. MODULE_DEVICE_TABLE(acpi, virtio_khv_mmio_acpi_match);
  588. #endif
  589. static struct platform_driver virtio_khv_mmio_driver = {
  590. .probe = virtio_khv_mmio_probe,
  591. .remove = virtio_khv_mmio_remove,
  592. .driver = {
  593. .name = "virtio-khv-mmio",
  594. .of_match_table = virtio_khv_mmio_match,
  595. .acpi_match_table = ACPI_PTR(virtio_khv_mmio_acpi_match),
  596. },
  597. };
  598. static int __init virtio_khv_mmio_init(void)
  599. {
  600. return platform_driver_register(&virtio_khv_mmio_driver);
  601. }
  602. static void __exit virtio_khv_mmio_exit(void)
  603. {
  604. platform_driver_unregister(&virtio_khv_mmio_driver);
  605. vm_unregister_cmdline_devices();
  606. }
  607. module_init(virtio_khv_mmio_init);
  608. module_exit(virtio_khv_mmio_exit);
  609. MODULE_AUTHOR("Xianting Tian <xianting.tian@linux.alibaba.com>");
  610. MODULE_DESCRIPTION("Platform bus driver for khv memory mapped virtio devices");
  611. MODULE_LICENSE("GPL");