job.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Tegra host1x Job
  4. *
  5. * Copyright (c) 2010-2015, NVIDIA Corporation.
  6. */
  7. #include <linux/dma-mapping.h>
  8. #include <linux/err.h>
  9. #include <linux/host1x.h>
  10. #include <linux/kref.h>
  11. #include <linux/module.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/slab.h>
  14. #include <linux/vmalloc.h>
  15. #include <trace/events/host1x.h>
  16. #include "channel.h"
  17. #include "dev.h"
  18. #include "job.h"
  19. #include "syncpt.h"
  20. #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
  21. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  22. u32 num_cmdbufs, u32 num_relocs)
  23. {
  24. struct host1x_job *job = NULL;
  25. unsigned int num_unpins = num_cmdbufs + num_relocs;
  26. u64 total;
  27. void *mem;
  28. /* Check that we're not going to overflow */
  29. total = sizeof(struct host1x_job) +
  30. (u64)num_relocs * sizeof(struct host1x_reloc) +
  31. (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
  32. (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
  33. (u64)num_unpins * sizeof(dma_addr_t) +
  34. (u64)num_unpins * sizeof(u32 *);
  35. if (total > ULONG_MAX)
  36. return NULL;
  37. mem = job = kzalloc(total, GFP_KERNEL);
  38. if (!job)
  39. return NULL;
  40. kref_init(&job->ref);
  41. job->channel = ch;
  42. /* Redistribute memory to the structs */
  43. mem += sizeof(struct host1x_job);
  44. job->relocs = num_relocs ? mem : NULL;
  45. mem += num_relocs * sizeof(struct host1x_reloc);
  46. job->unpins = num_unpins ? mem : NULL;
  47. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  48. job->gathers = num_cmdbufs ? mem : NULL;
  49. mem += num_cmdbufs * sizeof(struct host1x_job_gather);
  50. job->addr_phys = num_unpins ? mem : NULL;
  51. job->reloc_addr_phys = job->addr_phys;
  52. job->gather_addr_phys = &job->addr_phys[num_relocs];
  53. return job;
  54. }
  55. EXPORT_SYMBOL(host1x_job_alloc);
  56. struct host1x_job *host1x_job_get(struct host1x_job *job)
  57. {
  58. kref_get(&job->ref);
  59. return job;
  60. }
  61. EXPORT_SYMBOL(host1x_job_get);
  62. static void job_free(struct kref *ref)
  63. {
  64. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  65. kfree(job);
  66. }
  67. void host1x_job_put(struct host1x_job *job)
  68. {
  69. kref_put(&job->ref, job_free);
  70. }
  71. EXPORT_SYMBOL(host1x_job_put);
  72. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  73. unsigned int words, unsigned int offset)
  74. {
  75. struct host1x_job_gather *gather = &job->gathers[job->num_gathers];
  76. gather->words = words;
  77. gather->bo = bo;
  78. gather->offset = offset;
  79. job->num_gathers++;
  80. }
  81. EXPORT_SYMBOL(host1x_job_add_gather);
  82. static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
  83. {
  84. unsigned int i;
  85. int err;
  86. job->num_unpins = 0;
  87. for (i = 0; i < job->num_relocs; i++) {
  88. struct host1x_reloc *reloc = &job->relocs[i];
  89. struct sg_table *sgt;
  90. dma_addr_t phys_addr;
  91. reloc->target.bo = host1x_bo_get(reloc->target.bo);
  92. if (!reloc->target.bo) {
  93. err = -EINVAL;
  94. goto unpin;
  95. }
  96. phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
  97. job->addr_phys[job->num_unpins] = phys_addr;
  98. job->unpins[job->num_unpins].bo = reloc->target.bo;
  99. job->unpins[job->num_unpins].sgt = sgt;
  100. job->num_unpins++;
  101. }
  102. for (i = 0; i < job->num_gathers; i++) {
  103. struct host1x_job_gather *g = &job->gathers[i];
  104. size_t gather_size = 0;
  105. struct scatterlist *sg;
  106. struct sg_table *sgt;
  107. dma_addr_t phys_addr;
  108. unsigned long shift;
  109. struct iova *alloc;
  110. unsigned int j;
  111. g->bo = host1x_bo_get(g->bo);
  112. if (!g->bo) {
  113. err = -EINVAL;
  114. goto unpin;
  115. }
  116. phys_addr = host1x_bo_pin(g->bo, &sgt);
  117. if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && host->domain) {
  118. for_each_sg(sgt->sgl, sg, sgt->nents, j)
  119. gather_size += sg->length;
  120. gather_size = iova_align(&host->iova, gather_size);
  121. shift = iova_shift(&host->iova);
  122. alloc = alloc_iova(&host->iova, gather_size >> shift,
  123. host->iova_end >> shift, true);
  124. if (!alloc) {
  125. err = -ENOMEM;
  126. goto unpin;
  127. }
  128. err = iommu_map_sg(host->domain,
  129. iova_dma_addr(&host->iova, alloc),
  130. sgt->sgl, sgt->nents, IOMMU_READ);
  131. if (err == 0) {
  132. __free_iova(&host->iova, alloc);
  133. err = -EINVAL;
  134. goto unpin;
  135. }
  136. job->addr_phys[job->num_unpins] =
  137. iova_dma_addr(&host->iova, alloc);
  138. job->unpins[job->num_unpins].size = gather_size;
  139. } else {
  140. job->addr_phys[job->num_unpins] = phys_addr;
  141. }
  142. job->gather_addr_phys[i] = job->addr_phys[job->num_unpins];
  143. job->unpins[job->num_unpins].bo = g->bo;
  144. job->unpins[job->num_unpins].sgt = sgt;
  145. job->num_unpins++;
  146. }
  147. return 0;
  148. unpin:
  149. host1x_job_unpin(job);
  150. return err;
  151. }
  152. static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
  153. {
  154. u32 last_page = ~0;
  155. void *cmdbuf_page_addr = NULL;
  156. struct host1x_bo *cmdbuf = g->bo;
  157. unsigned int i;
  158. /* pin & patch the relocs for one gather */
  159. for (i = 0; i < job->num_relocs; i++) {
  160. struct host1x_reloc *reloc = &job->relocs[i];
  161. u32 reloc_addr = (job->reloc_addr_phys[i] +
  162. reloc->target.offset) >> reloc->shift;
  163. u32 *target;
  164. /* skip all other gathers */
  165. if (cmdbuf != reloc->cmdbuf.bo)
  166. continue;
  167. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
  168. target = (u32 *)job->gather_copy_mapped +
  169. reloc->cmdbuf.offset / sizeof(u32) +
  170. g->offset / sizeof(u32);
  171. goto patch_reloc;
  172. }
  173. if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
  174. if (cmdbuf_page_addr)
  175. host1x_bo_kunmap(cmdbuf, last_page,
  176. cmdbuf_page_addr);
  177. cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
  178. reloc->cmdbuf.offset >> PAGE_SHIFT);
  179. last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
  180. if (unlikely(!cmdbuf_page_addr)) {
  181. pr_err("Could not map cmdbuf for relocation\n");
  182. return -ENOMEM;
  183. }
  184. }
  185. target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
  186. patch_reloc:
  187. *target = reloc_addr;
  188. }
  189. if (cmdbuf_page_addr)
  190. host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
  191. return 0;
  192. }
  193. static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  194. unsigned int offset)
  195. {
  196. offset *= sizeof(u32);
  197. if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
  198. return false;
  199. /* relocation shift value validation isn't implemented yet */
  200. if (reloc->shift)
  201. return false;
  202. return true;
  203. }
  204. struct host1x_firewall {
  205. struct host1x_job *job;
  206. struct device *dev;
  207. unsigned int num_relocs;
  208. struct host1x_reloc *reloc;
  209. struct host1x_bo *cmdbuf;
  210. unsigned int offset;
  211. u32 words;
  212. u32 class;
  213. u32 reg;
  214. u32 mask;
  215. u32 count;
  216. };
  217. static int check_register(struct host1x_firewall *fw, unsigned long offset)
  218. {
  219. if (!fw->job->is_addr_reg)
  220. return 0;
  221. if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
  222. if (!fw->num_relocs)
  223. return -EINVAL;
  224. if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
  225. return -EINVAL;
  226. fw->num_relocs--;
  227. fw->reloc++;
  228. }
  229. return 0;
  230. }
  231. static int check_class(struct host1x_firewall *fw, u32 class)
  232. {
  233. if (!fw->job->is_valid_class) {
  234. if (fw->class != class)
  235. return -EINVAL;
  236. } else {
  237. if (!fw->job->is_valid_class(fw->class))
  238. return -EINVAL;
  239. }
  240. return 0;
  241. }
  242. static int check_mask(struct host1x_firewall *fw)
  243. {
  244. u32 mask = fw->mask;
  245. u32 reg = fw->reg;
  246. int ret;
  247. while (mask) {
  248. if (fw->words == 0)
  249. return -EINVAL;
  250. if (mask & 1) {
  251. ret = check_register(fw, reg);
  252. if (ret < 0)
  253. return ret;
  254. fw->words--;
  255. fw->offset++;
  256. }
  257. mask >>= 1;
  258. reg++;
  259. }
  260. return 0;
  261. }
  262. static int check_incr(struct host1x_firewall *fw)
  263. {
  264. u32 count = fw->count;
  265. u32 reg = fw->reg;
  266. int ret;
  267. while (count) {
  268. if (fw->words == 0)
  269. return -EINVAL;
  270. ret = check_register(fw, reg);
  271. if (ret < 0)
  272. return ret;
  273. reg++;
  274. fw->words--;
  275. fw->offset++;
  276. count--;
  277. }
  278. return 0;
  279. }
  280. static int check_nonincr(struct host1x_firewall *fw)
  281. {
  282. u32 count = fw->count;
  283. int ret;
  284. while (count) {
  285. if (fw->words == 0)
  286. return -EINVAL;
  287. ret = check_register(fw, fw->reg);
  288. if (ret < 0)
  289. return ret;
  290. fw->words--;
  291. fw->offset++;
  292. count--;
  293. }
  294. return 0;
  295. }
  296. static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
  297. {
  298. u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
  299. (g->offset / sizeof(u32));
  300. u32 job_class = fw->class;
  301. int err = 0;
  302. fw->words = g->words;
  303. fw->cmdbuf = g->bo;
  304. fw->offset = 0;
  305. while (fw->words && !err) {
  306. u32 word = cmdbuf_base[fw->offset];
  307. u32 opcode = (word & 0xf0000000) >> 28;
  308. fw->mask = 0;
  309. fw->reg = 0;
  310. fw->count = 0;
  311. fw->words--;
  312. fw->offset++;
  313. switch (opcode) {
  314. case 0:
  315. fw->class = word >> 6 & 0x3ff;
  316. fw->mask = word & 0x3f;
  317. fw->reg = word >> 16 & 0xfff;
  318. err = check_class(fw, job_class);
  319. if (!err)
  320. err = check_mask(fw);
  321. if (err)
  322. goto out;
  323. break;
  324. case 1:
  325. fw->reg = word >> 16 & 0xfff;
  326. fw->count = word & 0xffff;
  327. err = check_incr(fw);
  328. if (err)
  329. goto out;
  330. break;
  331. case 2:
  332. fw->reg = word >> 16 & 0xfff;
  333. fw->count = word & 0xffff;
  334. err = check_nonincr(fw);
  335. if (err)
  336. goto out;
  337. break;
  338. case 3:
  339. fw->mask = word & 0xffff;
  340. fw->reg = word >> 16 & 0xfff;
  341. err = check_mask(fw);
  342. if (err)
  343. goto out;
  344. break;
  345. case 4:
  346. case 14:
  347. break;
  348. default:
  349. err = -EINVAL;
  350. break;
  351. }
  352. }
  353. out:
  354. return err;
  355. }
  356. static inline int copy_gathers(struct device *host, struct host1x_job *job,
  357. struct device *dev)
  358. {
  359. struct host1x_firewall fw;
  360. size_t size = 0;
  361. size_t offset = 0;
  362. unsigned int i;
  363. fw.job = job;
  364. fw.dev = dev;
  365. fw.reloc = job->relocs;
  366. fw.num_relocs = job->num_relocs;
  367. fw.class = job->class;
  368. for (i = 0; i < job->num_gathers; i++) {
  369. struct host1x_job_gather *g = &job->gathers[i];
  370. size += g->words * sizeof(u32);
  371. }
  372. /*
  373. * Try a non-blocking allocation from a higher priority pools first,
  374. * as awaiting for the allocation here is a major performance hit.
  375. */
  376. job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
  377. GFP_NOWAIT);
  378. /* the higher priority allocation failed, try the generic-blocking */
  379. if (!job->gather_copy_mapped)
  380. job->gather_copy_mapped = dma_alloc_wc(host, size,
  381. &job->gather_copy,
  382. GFP_KERNEL);
  383. if (!job->gather_copy_mapped)
  384. return -ENOMEM;
  385. job->gather_copy_size = size;
  386. for (i = 0; i < job->num_gathers; i++) {
  387. struct host1x_job_gather *g = &job->gathers[i];
  388. void *gather;
  389. /* Copy the gather */
  390. gather = host1x_bo_mmap(g->bo);
  391. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  392. g->words * sizeof(u32));
  393. host1x_bo_munmap(g->bo, gather);
  394. /* Store the location in the buffer */
  395. g->base = job->gather_copy;
  396. g->offset = offset;
  397. /* Validate the job */
  398. if (validate(&fw, g))
  399. return -EINVAL;
  400. offset += g->words * sizeof(u32);
  401. }
  402. /* No relocs should remain at this point */
  403. if (fw.num_relocs)
  404. return -EINVAL;
  405. return 0;
  406. }
  407. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  408. {
  409. int err;
  410. unsigned int i, j;
  411. struct host1x *host = dev_get_drvdata(dev->parent);
  412. /* pin memory */
  413. err = pin_job(host, job);
  414. if (err)
  415. goto out;
  416. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL)) {
  417. err = copy_gathers(host->dev, job, dev);
  418. if (err)
  419. goto out;
  420. }
  421. /* patch gathers */
  422. for (i = 0; i < job->num_gathers; i++) {
  423. struct host1x_job_gather *g = &job->gathers[i];
  424. /* process each gather mem only once */
  425. if (g->handled)
  426. continue;
  427. /* copy_gathers() sets gathers base if firewall is enabled */
  428. if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL))
  429. g->base = job->gather_addr_phys[i];
  430. for (j = i + 1; j < job->num_gathers; j++) {
  431. if (job->gathers[j].bo == g->bo) {
  432. job->gathers[j].handled = true;
  433. job->gathers[j].base = g->base;
  434. }
  435. }
  436. err = do_relocs(job, g);
  437. if (err)
  438. break;
  439. }
  440. out:
  441. if (err)
  442. host1x_job_unpin(job);
  443. wmb();
  444. return err;
  445. }
  446. EXPORT_SYMBOL(host1x_job_pin);
  447. void host1x_job_unpin(struct host1x_job *job)
  448. {
  449. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  450. unsigned int i;
  451. for (i = 0; i < job->num_unpins; i++) {
  452. struct host1x_job_unpin_data *unpin = &job->unpins[i];
  453. if (!IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) &&
  454. unpin->size && host->domain) {
  455. iommu_unmap(host->domain, job->addr_phys[i],
  456. unpin->size);
  457. free_iova(&host->iova,
  458. iova_pfn(&host->iova, job->addr_phys[i]));
  459. }
  460. host1x_bo_unpin(unpin->bo, unpin->sgt);
  461. host1x_bo_put(unpin->bo);
  462. }
  463. job->num_unpins = 0;
  464. if (job->gather_copy_size)
  465. dma_free_wc(host->dev, job->gather_copy_size,
  466. job->gather_copy_mapped, job->gather_copy);
  467. }
  468. EXPORT_SYMBOL(host1x_job_unpin);
  469. /*
  470. * Debug routine used to dump job entries
  471. */
  472. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  473. {
  474. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
  475. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  476. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  477. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  478. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  479. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  480. }