shmobile-iommu.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * IOMMU for IPMMU/IPMMUI
  3. * Copyright (C) 2012 Hideki EIRAKU
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. */
  9. #include <linux/dma-mapping.h>
  10. #include <linux/io.h>
  11. #include <linux/iommu.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/sizes.h>
  14. #include <linux/slab.h>
  15. #include <asm/dma-iommu.h>
  16. #include "shmobile-ipmmu.h"
  17. #define L1_SIZE CONFIG_SHMOBILE_IOMMU_L1SIZE
  18. #define L1_LEN (L1_SIZE / 4)
  19. #define L1_ALIGN L1_SIZE
  20. #define L2_SIZE SZ_1K
  21. #define L2_LEN (L2_SIZE / 4)
  22. #define L2_ALIGN L2_SIZE
  23. struct shmobile_iommu_domain_pgtable {
  24. uint32_t *pgtable;
  25. dma_addr_t handle;
  26. };
  27. struct shmobile_iommu_archdata {
  28. struct list_head attached_list;
  29. struct dma_iommu_mapping *iommu_mapping;
  30. spinlock_t attach_lock;
  31. struct shmobile_iommu_domain *attached;
  32. int num_attached_devices;
  33. struct shmobile_ipmmu *ipmmu;
  34. };
  35. struct shmobile_iommu_domain {
  36. struct shmobile_iommu_domain_pgtable l1, l2[L1_LEN];
  37. spinlock_t map_lock;
  38. spinlock_t attached_list_lock;
  39. struct list_head attached_list;
  40. };
  41. static struct shmobile_iommu_archdata *ipmmu_archdata;
  42. static struct kmem_cache *l1cache, *l2cache;
  43. static int pgtable_alloc(struct shmobile_iommu_domain_pgtable *pgtable,
  44. struct kmem_cache *cache, size_t size)
  45. {
  46. pgtable->pgtable = kmem_cache_zalloc(cache, GFP_ATOMIC);
  47. if (!pgtable->pgtable)
  48. return -ENOMEM;
  49. pgtable->handle = dma_map_single(NULL, pgtable->pgtable, size,
  50. DMA_TO_DEVICE);
  51. return 0;
  52. }
  53. static void pgtable_free(struct shmobile_iommu_domain_pgtable *pgtable,
  54. struct kmem_cache *cache, size_t size)
  55. {
  56. dma_unmap_single(NULL, pgtable->handle, size, DMA_TO_DEVICE);
  57. kmem_cache_free(cache, pgtable->pgtable);
  58. }
  59. static uint32_t pgtable_read(struct shmobile_iommu_domain_pgtable *pgtable,
  60. unsigned int index)
  61. {
  62. return pgtable->pgtable[index];
  63. }
  64. static void pgtable_write(struct shmobile_iommu_domain_pgtable *pgtable,
  65. unsigned int index, unsigned int count, uint32_t val)
  66. {
  67. unsigned int i;
  68. for (i = 0; i < count; i++)
  69. pgtable->pgtable[index + i] = val;
  70. dma_sync_single_for_device(NULL, pgtable->handle + index * sizeof(val),
  71. sizeof(val) * count, DMA_TO_DEVICE);
  72. }
  73. static int shmobile_iommu_domain_init(struct iommu_domain *domain)
  74. {
  75. struct shmobile_iommu_domain *sh_domain;
  76. int i, ret;
  77. sh_domain = kmalloc(sizeof(*sh_domain), GFP_KERNEL);
  78. if (!sh_domain)
  79. return -ENOMEM;
  80. ret = pgtable_alloc(&sh_domain->l1, l1cache, L1_SIZE);
  81. if (ret < 0) {
  82. kfree(sh_domain);
  83. return ret;
  84. }
  85. for (i = 0; i < L1_LEN; i++)
  86. sh_domain->l2[i].pgtable = NULL;
  87. spin_lock_init(&sh_domain->map_lock);
  88. spin_lock_init(&sh_domain->attached_list_lock);
  89. INIT_LIST_HEAD(&sh_domain->attached_list);
  90. domain->priv = sh_domain;
  91. return 0;
  92. }
  93. static void shmobile_iommu_domain_destroy(struct iommu_domain *domain)
  94. {
  95. struct shmobile_iommu_domain *sh_domain = domain->priv;
  96. int i;
  97. for (i = 0; i < L1_LEN; i++) {
  98. if (sh_domain->l2[i].pgtable)
  99. pgtable_free(&sh_domain->l2[i], l2cache, L2_SIZE);
  100. }
  101. pgtable_free(&sh_domain->l1, l1cache, L1_SIZE);
  102. kfree(sh_domain);
  103. domain->priv = NULL;
  104. }
  105. static int shmobile_iommu_attach_device(struct iommu_domain *domain,
  106. struct device *dev)
  107. {
  108. struct shmobile_iommu_archdata *archdata = dev->archdata.iommu;
  109. struct shmobile_iommu_domain *sh_domain = domain->priv;
  110. int ret = -EBUSY;
  111. if (!archdata)
  112. return -ENODEV;
  113. spin_lock(&sh_domain->attached_list_lock);
  114. spin_lock(&archdata->attach_lock);
  115. if (archdata->attached != sh_domain) {
  116. if (archdata->attached)
  117. goto err;
  118. ipmmu_tlb_set(archdata->ipmmu, sh_domain->l1.handle, L1_SIZE,
  119. 0);
  120. ipmmu_tlb_flush(archdata->ipmmu);
  121. archdata->attached = sh_domain;
  122. archdata->num_attached_devices = 0;
  123. list_add(&archdata->attached_list, &sh_domain->attached_list);
  124. }
  125. archdata->num_attached_devices++;
  126. ret = 0;
  127. err:
  128. spin_unlock(&archdata->attach_lock);
  129. spin_unlock(&sh_domain->attached_list_lock);
  130. return ret;
  131. }
  132. static void shmobile_iommu_detach_device(struct iommu_domain *domain,
  133. struct device *dev)
  134. {
  135. struct shmobile_iommu_archdata *archdata = dev->archdata.iommu;
  136. struct shmobile_iommu_domain *sh_domain = domain->priv;
  137. if (!archdata)
  138. return;
  139. spin_lock(&sh_domain->attached_list_lock);
  140. spin_lock(&archdata->attach_lock);
  141. archdata->num_attached_devices--;
  142. if (!archdata->num_attached_devices) {
  143. ipmmu_tlb_set(archdata->ipmmu, 0, 0, 0);
  144. ipmmu_tlb_flush(archdata->ipmmu);
  145. archdata->attached = NULL;
  146. list_del(&archdata->attached_list);
  147. }
  148. spin_unlock(&archdata->attach_lock);
  149. spin_unlock(&sh_domain->attached_list_lock);
  150. }
  151. static void domain_tlb_flush(struct shmobile_iommu_domain *sh_domain)
  152. {
  153. struct shmobile_iommu_archdata *archdata;
  154. spin_lock(&sh_domain->attached_list_lock);
  155. list_for_each_entry(archdata, &sh_domain->attached_list, attached_list)
  156. ipmmu_tlb_flush(archdata->ipmmu);
  157. spin_unlock(&sh_domain->attached_list_lock);
  158. }
  159. static int l2alloc(struct shmobile_iommu_domain *sh_domain,
  160. unsigned int l1index)
  161. {
  162. int ret;
  163. if (!sh_domain->l2[l1index].pgtable) {
  164. ret = pgtable_alloc(&sh_domain->l2[l1index], l2cache, L2_SIZE);
  165. if (ret < 0)
  166. return ret;
  167. }
  168. pgtable_write(&sh_domain->l1, l1index, 1,
  169. sh_domain->l2[l1index].handle | 0x1);
  170. return 0;
  171. }
  172. static void l2realfree(struct shmobile_iommu_domain_pgtable *l2)
  173. {
  174. if (l2->pgtable)
  175. pgtable_free(l2, l2cache, L2_SIZE);
  176. }
  177. static void l2free(struct shmobile_iommu_domain *sh_domain,
  178. unsigned int l1index,
  179. struct shmobile_iommu_domain_pgtable *l2)
  180. {
  181. pgtable_write(&sh_domain->l1, l1index, 1, 0);
  182. if (sh_domain->l2[l1index].pgtable) {
  183. *l2 = sh_domain->l2[l1index];
  184. sh_domain->l2[l1index].pgtable = NULL;
  185. }
  186. }
  187. static int shmobile_iommu_map(struct iommu_domain *domain, unsigned long iova,
  188. phys_addr_t paddr, size_t size, int prot)
  189. {
  190. struct shmobile_iommu_domain_pgtable l2 = { .pgtable = NULL };
  191. struct shmobile_iommu_domain *sh_domain = domain->priv;
  192. unsigned int l1index, l2index;
  193. int ret;
  194. l1index = iova >> 20;
  195. switch (size) {
  196. case SZ_4K:
  197. l2index = (iova >> 12) & 0xff;
  198. spin_lock(&sh_domain->map_lock);
  199. ret = l2alloc(sh_domain, l1index);
  200. if (!ret)
  201. pgtable_write(&sh_domain->l2[l1index], l2index, 1,
  202. paddr | 0xff2);
  203. spin_unlock(&sh_domain->map_lock);
  204. break;
  205. case SZ_64K:
  206. l2index = (iova >> 12) & 0xf0;
  207. spin_lock(&sh_domain->map_lock);
  208. ret = l2alloc(sh_domain, l1index);
  209. if (!ret)
  210. pgtable_write(&sh_domain->l2[l1index], l2index, 0x10,
  211. paddr | 0xff1);
  212. spin_unlock(&sh_domain->map_lock);
  213. break;
  214. case SZ_1M:
  215. spin_lock(&sh_domain->map_lock);
  216. l2free(sh_domain, l1index, &l2);
  217. pgtable_write(&sh_domain->l1, l1index, 1, paddr | 0xc02);
  218. spin_unlock(&sh_domain->map_lock);
  219. ret = 0;
  220. break;
  221. default:
  222. ret = -EINVAL;
  223. }
  224. if (!ret)
  225. domain_tlb_flush(sh_domain);
  226. l2realfree(&l2);
  227. return ret;
  228. }
  229. static size_t shmobile_iommu_unmap(struct iommu_domain *domain,
  230. unsigned long iova, size_t size)
  231. {
  232. struct shmobile_iommu_domain_pgtable l2 = { .pgtable = NULL };
  233. struct shmobile_iommu_domain *sh_domain = domain->priv;
  234. unsigned int l1index, l2index;
  235. uint32_t l2entry = 0;
  236. size_t ret = 0;
  237. l1index = iova >> 20;
  238. if (!(iova & 0xfffff) && size >= SZ_1M) {
  239. spin_lock(&sh_domain->map_lock);
  240. l2free(sh_domain, l1index, &l2);
  241. spin_unlock(&sh_domain->map_lock);
  242. ret = SZ_1M;
  243. goto done;
  244. }
  245. l2index = (iova >> 12) & 0xff;
  246. spin_lock(&sh_domain->map_lock);
  247. if (sh_domain->l2[l1index].pgtable)
  248. l2entry = pgtable_read(&sh_domain->l2[l1index], l2index);
  249. switch (l2entry & 3) {
  250. case 1:
  251. if (l2index & 0xf)
  252. break;
  253. pgtable_write(&sh_domain->l2[l1index], l2index, 0x10, 0);
  254. ret = SZ_64K;
  255. break;
  256. case 2:
  257. pgtable_write(&sh_domain->l2[l1index], l2index, 1, 0);
  258. ret = SZ_4K;
  259. break;
  260. }
  261. spin_unlock(&sh_domain->map_lock);
  262. done:
  263. if (ret)
  264. domain_tlb_flush(sh_domain);
  265. l2realfree(&l2);
  266. return ret;
  267. }
  268. static phys_addr_t shmobile_iommu_iova_to_phys(struct iommu_domain *domain,
  269. unsigned long iova)
  270. {
  271. struct shmobile_iommu_domain *sh_domain = domain->priv;
  272. uint32_t l1entry = 0, l2entry = 0;
  273. unsigned int l1index, l2index;
  274. l1index = iova >> 20;
  275. l2index = (iova >> 12) & 0xff;
  276. spin_lock(&sh_domain->map_lock);
  277. if (sh_domain->l2[l1index].pgtable)
  278. l2entry = pgtable_read(&sh_domain->l2[l1index], l2index);
  279. else
  280. l1entry = pgtable_read(&sh_domain->l1, l1index);
  281. spin_unlock(&sh_domain->map_lock);
  282. switch (l2entry & 3) {
  283. case 1:
  284. return (l2entry & ~0xffff) | (iova & 0xffff);
  285. case 2:
  286. return (l2entry & ~0xfff) | (iova & 0xfff);
  287. default:
  288. if ((l1entry & 3) == 2)
  289. return (l1entry & ~0xfffff) | (iova & 0xfffff);
  290. return 0;
  291. }
  292. }
  293. static int find_dev_name(struct shmobile_ipmmu *ipmmu, const char *dev_name)
  294. {
  295. unsigned int i, n = ipmmu->num_dev_names;
  296. for (i = 0; i < n; i++) {
  297. if (strcmp(ipmmu->dev_names[i], dev_name) == 0)
  298. return 1;
  299. }
  300. return 0;
  301. }
  302. static int shmobile_iommu_add_device(struct device *dev)
  303. {
  304. struct shmobile_iommu_archdata *archdata = ipmmu_archdata;
  305. struct dma_iommu_mapping *mapping;
  306. if (!find_dev_name(archdata->ipmmu, dev_name(dev)))
  307. return 0;
  308. mapping = archdata->iommu_mapping;
  309. if (!mapping) {
  310. mapping = arm_iommu_create_mapping(&platform_bus_type, 0,
  311. L1_LEN << 20, 0);
  312. if (IS_ERR(mapping))
  313. return PTR_ERR(mapping);
  314. archdata->iommu_mapping = mapping;
  315. }
  316. dev->archdata.iommu = archdata;
  317. if (arm_iommu_attach_device(dev, mapping))
  318. pr_err("arm_iommu_attach_device failed\n");
  319. return 0;
  320. }
  321. static struct iommu_ops shmobile_iommu_ops = {
  322. .domain_init = shmobile_iommu_domain_init,
  323. .domain_destroy = shmobile_iommu_domain_destroy,
  324. .attach_dev = shmobile_iommu_attach_device,
  325. .detach_dev = shmobile_iommu_detach_device,
  326. .map = shmobile_iommu_map,
  327. .unmap = shmobile_iommu_unmap,
  328. .iova_to_phys = shmobile_iommu_iova_to_phys,
  329. .add_device = shmobile_iommu_add_device,
  330. .pgsize_bitmap = SZ_1M | SZ_64K | SZ_4K,
  331. };
  332. int ipmmu_iommu_init(struct shmobile_ipmmu *ipmmu)
  333. {
  334. static struct shmobile_iommu_archdata *archdata;
  335. l1cache = kmem_cache_create("shmobile-iommu-pgtable1", L1_SIZE,
  336. L1_ALIGN, SLAB_HWCACHE_ALIGN, NULL);
  337. if (!l1cache)
  338. return -ENOMEM;
  339. l2cache = kmem_cache_create("shmobile-iommu-pgtable2", L2_SIZE,
  340. L2_ALIGN, SLAB_HWCACHE_ALIGN, NULL);
  341. if (!l2cache) {
  342. kmem_cache_destroy(l1cache);
  343. return -ENOMEM;
  344. }
  345. archdata = kmalloc(sizeof(*archdata), GFP_KERNEL);
  346. if (!archdata) {
  347. kmem_cache_destroy(l1cache);
  348. kmem_cache_destroy(l2cache);
  349. return -ENOMEM;
  350. }
  351. spin_lock_init(&archdata->attach_lock);
  352. archdata->attached = NULL;
  353. archdata->ipmmu = ipmmu;
  354. ipmmu_archdata = archdata;
  355. bus_set_iommu(&platform_bus_type, &shmobile_iommu_ops);
  356. return 0;
  357. }