job.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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/iommu.h>
  11. #include <linux/kref.h>
  12. #include <linux/module.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/slab.h>
  15. #include <linux/vmalloc.h>
  16. #include <trace/events/host1x.h>
  17. #include "channel.h"
  18. #include "dev.h"
  19. #include "job.h"
  20. #include "syncpt.h"
  21. #define HOST1X_WAIT_SYNCPT_OFFSET 0x8
  22. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  23. u32 num_cmdbufs, u32 num_relocs,
  24. bool skip_firewall)
  25. {
  26. struct host1x_job *job = NULL;
  27. unsigned int num_unpins = num_relocs;
  28. bool enable_firewall;
  29. u64 total;
  30. void *mem;
  31. enable_firewall = IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !skip_firewall;
  32. if (!enable_firewall)
  33. num_unpins += num_cmdbufs;
  34. /* Check that we're not going to overflow */
  35. total = sizeof(struct host1x_job) +
  36. (u64)num_relocs * sizeof(struct host1x_reloc) +
  37. (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
  38. (u64)num_cmdbufs * sizeof(struct host1x_job_cmd) +
  39. (u64)num_unpins * sizeof(dma_addr_t) +
  40. (u64)num_unpins * sizeof(u32 *);
  41. if (total > ULONG_MAX)
  42. return NULL;
  43. mem = job = kzalloc(total, GFP_KERNEL);
  44. if (!job)
  45. return NULL;
  46. job->enable_firewall = enable_firewall;
  47. kref_init(&job->ref);
  48. job->channel = ch;
  49. /* Redistribute memory to the structs */
  50. mem += sizeof(struct host1x_job);
  51. job->relocs = num_relocs ? mem : NULL;
  52. mem += num_relocs * sizeof(struct host1x_reloc);
  53. job->unpins = num_unpins ? mem : NULL;
  54. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  55. job->cmds = num_cmdbufs ? mem : NULL;
  56. mem += num_cmdbufs * sizeof(struct host1x_job_cmd);
  57. job->addr_phys = num_unpins ? mem : NULL;
  58. job->reloc_addr_phys = job->addr_phys;
  59. job->gather_addr_phys = &job->addr_phys[num_relocs];
  60. return job;
  61. }
  62. EXPORT_SYMBOL(host1x_job_alloc);
  63. struct host1x_job *host1x_job_get(struct host1x_job *job)
  64. {
  65. kref_get(&job->ref);
  66. return job;
  67. }
  68. EXPORT_SYMBOL(host1x_job_get);
  69. static void job_free(struct kref *ref)
  70. {
  71. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  72. if (job->release)
  73. job->release(job);
  74. if (job->fence) {
  75. /*
  76. * remove_callback is atomic w.r.t. fence signaling, so
  77. * after the call returns, we know that the callback is not
  78. * in execution, and the fence can be safely freed.
  79. */
  80. dma_fence_remove_callback(job->fence, &job->fence_cb);
  81. dma_fence_put(job->fence);
  82. }
  83. if (job->syncpt)
  84. host1x_syncpt_put(job->syncpt);
  85. kfree(job);
  86. }
  87. void host1x_job_put(struct host1x_job *job)
  88. {
  89. kref_put(&job->ref, job_free);
  90. }
  91. EXPORT_SYMBOL(host1x_job_put);
  92. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  93. unsigned int words, unsigned int offset)
  94. {
  95. struct host1x_job_gather *gather = &job->cmds[job->num_cmds].gather;
  96. gather->words = words;
  97. gather->bo = bo;
  98. gather->offset = offset;
  99. job->num_cmds++;
  100. }
  101. EXPORT_SYMBOL(host1x_job_add_gather);
  102. void host1x_job_add_wait(struct host1x_job *job, u32 id, u32 thresh,
  103. bool relative, u32 next_class)
  104. {
  105. struct host1x_job_cmd *cmd = &job->cmds[job->num_cmds];
  106. cmd->is_wait = true;
  107. cmd->wait.id = id;
  108. cmd->wait.threshold = thresh;
  109. cmd->wait.next_class = next_class;
  110. cmd->wait.relative = relative;
  111. job->num_cmds++;
  112. }
  113. EXPORT_SYMBOL(host1x_job_add_wait);
  114. static unsigned int pin_job(struct host1x *host, struct host1x_job *job)
  115. {
  116. unsigned long mask = HOST1X_RELOC_READ | HOST1X_RELOC_WRITE;
  117. struct host1x_client *client = job->client;
  118. struct device *dev = client->dev;
  119. struct host1x_job_gather *g;
  120. unsigned int i;
  121. int err;
  122. job->num_unpins = 0;
  123. for (i = 0; i < job->num_relocs; i++) {
  124. struct host1x_reloc *reloc = &job->relocs[i];
  125. enum dma_data_direction direction;
  126. struct host1x_bo_mapping *map;
  127. struct host1x_bo *bo;
  128. reloc->target.bo = host1x_bo_get(reloc->target.bo);
  129. if (!reloc->target.bo) {
  130. err = -EINVAL;
  131. goto unpin;
  132. }
  133. bo = reloc->target.bo;
  134. switch (reloc->flags & mask) {
  135. case HOST1X_RELOC_READ:
  136. direction = DMA_TO_DEVICE;
  137. break;
  138. case HOST1X_RELOC_WRITE:
  139. direction = DMA_FROM_DEVICE;
  140. break;
  141. case HOST1X_RELOC_READ | HOST1X_RELOC_WRITE:
  142. direction = DMA_BIDIRECTIONAL;
  143. break;
  144. default:
  145. err = -EINVAL;
  146. goto unpin;
  147. }
  148. map = host1x_bo_pin(dev, bo, direction, NULL);
  149. if (IS_ERR(map)) {
  150. err = PTR_ERR(map);
  151. goto unpin;
  152. }
  153. /*
  154. * host1x clients are generally not able to do scatter-gather themselves, so fail
  155. * if the buffer is discontiguous and we fail to map its SG table to a single
  156. * contiguous chunk of I/O virtual memory.
  157. */
  158. if (map->chunks > 1) {
  159. err = -EINVAL;
  160. goto unpin;
  161. }
  162. job->addr_phys[job->num_unpins] = map->phys;
  163. job->unpins[job->num_unpins].map = map;
  164. job->num_unpins++;
  165. }
  166. /*
  167. * We will copy gathers BO content later, so there is no need to
  168. * hold and pin them.
  169. */
  170. if (job->enable_firewall)
  171. return 0;
  172. for (i = 0; i < job->num_cmds; i++) {
  173. struct host1x_bo_mapping *map;
  174. size_t gather_size = 0;
  175. struct scatterlist *sg;
  176. unsigned long shift;
  177. struct iova *alloc;
  178. unsigned int j;
  179. if (job->cmds[i].is_wait)
  180. continue;
  181. g = &job->cmds[i].gather;
  182. g->bo = host1x_bo_get(g->bo);
  183. if (!g->bo) {
  184. err = -EINVAL;
  185. goto unpin;
  186. }
  187. map = host1x_bo_pin(host->dev, g->bo, DMA_TO_DEVICE, NULL);
  188. if (IS_ERR(map)) {
  189. err = PTR_ERR(map);
  190. goto unpin;
  191. }
  192. if (host->domain) {
  193. for_each_sgtable_sg(map->sgt, sg, j)
  194. gather_size += sg->length;
  195. gather_size = iova_align(&host->iova, gather_size);
  196. shift = iova_shift(&host->iova);
  197. alloc = alloc_iova(&host->iova, gather_size >> shift,
  198. host->iova_end >> shift, true);
  199. if (!alloc) {
  200. err = -ENOMEM;
  201. goto put;
  202. }
  203. err = iommu_map_sgtable(host->domain, iova_dma_addr(&host->iova, alloc),
  204. map->sgt, IOMMU_READ);
  205. if (err == 0) {
  206. __free_iova(&host->iova, alloc);
  207. err = -EINVAL;
  208. goto put;
  209. }
  210. map->phys = iova_dma_addr(&host->iova, alloc);
  211. map->size = gather_size;
  212. }
  213. job->addr_phys[job->num_unpins] = map->phys;
  214. job->unpins[job->num_unpins].map = map;
  215. job->num_unpins++;
  216. job->gather_addr_phys[i] = map->phys;
  217. }
  218. return 0;
  219. put:
  220. host1x_bo_put(g->bo);
  221. unpin:
  222. host1x_job_unpin(job);
  223. return err;
  224. }
  225. static int do_relocs(struct host1x_job *job, struct host1x_job_gather *g)
  226. {
  227. void *cmdbuf_addr = NULL;
  228. struct host1x_bo *cmdbuf = g->bo;
  229. unsigned int i;
  230. /* pin & patch the relocs for one gather */
  231. for (i = 0; i < job->num_relocs; i++) {
  232. struct host1x_reloc *reloc = &job->relocs[i];
  233. u32 reloc_addr = (job->reloc_addr_phys[i] +
  234. reloc->target.offset) >> reloc->shift;
  235. u32 *target;
  236. /* skip all other gathers */
  237. if (cmdbuf != reloc->cmdbuf.bo)
  238. continue;
  239. if (job->enable_firewall) {
  240. target = (u32 *)job->gather_copy_mapped +
  241. reloc->cmdbuf.offset / sizeof(u32) +
  242. g->offset / sizeof(u32);
  243. goto patch_reloc;
  244. }
  245. if (!cmdbuf_addr) {
  246. cmdbuf_addr = host1x_bo_mmap(cmdbuf);
  247. if (unlikely(!cmdbuf_addr)) {
  248. pr_err("Could not map cmdbuf for relocation\n");
  249. return -ENOMEM;
  250. }
  251. }
  252. target = cmdbuf_addr + reloc->cmdbuf.offset;
  253. patch_reloc:
  254. *target = reloc_addr;
  255. }
  256. if (cmdbuf_addr)
  257. host1x_bo_munmap(cmdbuf, cmdbuf_addr);
  258. return 0;
  259. }
  260. static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  261. unsigned int offset)
  262. {
  263. offset *= sizeof(u32);
  264. if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
  265. return false;
  266. /* relocation shift value validation isn't implemented yet */
  267. if (reloc->shift)
  268. return false;
  269. return true;
  270. }
  271. struct host1x_firewall {
  272. struct host1x_job *job;
  273. struct device *dev;
  274. unsigned int num_relocs;
  275. struct host1x_reloc *reloc;
  276. struct host1x_bo *cmdbuf;
  277. unsigned int offset;
  278. u32 words;
  279. u32 class;
  280. u32 reg;
  281. u32 mask;
  282. u32 count;
  283. };
  284. static int check_register(struct host1x_firewall *fw, unsigned long offset)
  285. {
  286. if (!fw->job->is_addr_reg)
  287. return 0;
  288. if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
  289. if (!fw->num_relocs)
  290. return -EINVAL;
  291. if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
  292. return -EINVAL;
  293. fw->num_relocs--;
  294. fw->reloc++;
  295. }
  296. return 0;
  297. }
  298. static int check_class(struct host1x_firewall *fw, u32 class)
  299. {
  300. if (!fw->job->is_valid_class) {
  301. if (fw->class != class)
  302. return -EINVAL;
  303. } else {
  304. if (!fw->job->is_valid_class(fw->class))
  305. return -EINVAL;
  306. }
  307. return 0;
  308. }
  309. static int check_mask(struct host1x_firewall *fw)
  310. {
  311. u32 mask = fw->mask;
  312. u32 reg = fw->reg;
  313. int ret;
  314. while (mask) {
  315. if (fw->words == 0)
  316. return -EINVAL;
  317. if (mask & 1) {
  318. ret = check_register(fw, reg);
  319. if (ret < 0)
  320. return ret;
  321. fw->words--;
  322. fw->offset++;
  323. }
  324. mask >>= 1;
  325. reg++;
  326. }
  327. return 0;
  328. }
  329. static int check_incr(struct host1x_firewall *fw)
  330. {
  331. u32 count = fw->count;
  332. u32 reg = fw->reg;
  333. int ret;
  334. while (count) {
  335. if (fw->words == 0)
  336. return -EINVAL;
  337. ret = check_register(fw, reg);
  338. if (ret < 0)
  339. return ret;
  340. reg++;
  341. fw->words--;
  342. fw->offset++;
  343. count--;
  344. }
  345. return 0;
  346. }
  347. static int check_nonincr(struct host1x_firewall *fw)
  348. {
  349. u32 count = fw->count;
  350. int ret;
  351. while (count) {
  352. if (fw->words == 0)
  353. return -EINVAL;
  354. ret = check_register(fw, fw->reg);
  355. if (ret < 0)
  356. return ret;
  357. fw->words--;
  358. fw->offset++;
  359. count--;
  360. }
  361. return 0;
  362. }
  363. static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
  364. {
  365. u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
  366. (g->offset / sizeof(u32));
  367. u32 job_class = fw->class;
  368. int err = 0;
  369. fw->words = g->words;
  370. fw->cmdbuf = g->bo;
  371. fw->offset = 0;
  372. while (fw->words && !err) {
  373. u32 word = cmdbuf_base[fw->offset];
  374. u32 opcode = (word & 0xf0000000) >> 28;
  375. fw->mask = 0;
  376. fw->reg = 0;
  377. fw->count = 0;
  378. fw->words--;
  379. fw->offset++;
  380. switch (opcode) {
  381. case 0:
  382. fw->class = word >> 6 & 0x3ff;
  383. fw->mask = word & 0x3f;
  384. fw->reg = word >> 16 & 0xfff;
  385. err = check_class(fw, job_class);
  386. if (!err)
  387. err = check_mask(fw);
  388. if (err)
  389. goto out;
  390. break;
  391. case 1:
  392. fw->reg = word >> 16 & 0xfff;
  393. fw->count = word & 0xffff;
  394. err = check_incr(fw);
  395. if (err)
  396. goto out;
  397. break;
  398. case 2:
  399. fw->reg = word >> 16 & 0xfff;
  400. fw->count = word & 0xffff;
  401. err = check_nonincr(fw);
  402. if (err)
  403. goto out;
  404. break;
  405. case 3:
  406. fw->mask = word & 0xffff;
  407. fw->reg = word >> 16 & 0xfff;
  408. err = check_mask(fw);
  409. if (err)
  410. goto out;
  411. break;
  412. case 4:
  413. case 14:
  414. break;
  415. default:
  416. err = -EINVAL;
  417. break;
  418. }
  419. }
  420. out:
  421. return err;
  422. }
  423. static inline int copy_gathers(struct device *host, struct host1x_job *job,
  424. struct device *dev)
  425. {
  426. struct host1x_firewall fw;
  427. size_t size = 0;
  428. size_t offset = 0;
  429. unsigned int i;
  430. fw.job = job;
  431. fw.dev = dev;
  432. fw.reloc = job->relocs;
  433. fw.num_relocs = job->num_relocs;
  434. fw.class = job->class;
  435. for (i = 0; i < job->num_cmds; i++) {
  436. struct host1x_job_gather *g;
  437. if (job->cmds[i].is_wait)
  438. continue;
  439. g = &job->cmds[i].gather;
  440. size += g->words * sizeof(u32);
  441. }
  442. /*
  443. * Try a non-blocking allocation from a higher priority pools first,
  444. * as awaiting for the allocation here is a major performance hit.
  445. */
  446. job->gather_copy_mapped = dma_alloc_wc(host, size, &job->gather_copy,
  447. GFP_NOWAIT);
  448. /* the higher priority allocation failed, try the generic-blocking */
  449. if (!job->gather_copy_mapped)
  450. job->gather_copy_mapped = dma_alloc_wc(host, size,
  451. &job->gather_copy,
  452. GFP_KERNEL);
  453. if (!job->gather_copy_mapped)
  454. return -ENOMEM;
  455. job->gather_copy_size = size;
  456. for (i = 0; i < job->num_cmds; i++) {
  457. struct host1x_job_gather *g;
  458. void *gather;
  459. if (job->cmds[i].is_wait)
  460. continue;
  461. g = &job->cmds[i].gather;
  462. /* Copy the gather */
  463. gather = host1x_bo_mmap(g->bo);
  464. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  465. g->words * sizeof(u32));
  466. host1x_bo_munmap(g->bo, gather);
  467. /* Store the location in the buffer */
  468. g->base = job->gather_copy;
  469. g->offset = offset;
  470. /* Validate the job */
  471. if (validate(&fw, g))
  472. return -EINVAL;
  473. offset += g->words * sizeof(u32);
  474. }
  475. /* No relocs should remain at this point */
  476. if (fw.num_relocs)
  477. return -EINVAL;
  478. return 0;
  479. }
  480. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  481. {
  482. int err;
  483. unsigned int i, j;
  484. struct host1x *host = dev_get_drvdata(dev->parent);
  485. /* pin memory */
  486. err = pin_job(host, job);
  487. if (err)
  488. goto out;
  489. if (job->enable_firewall) {
  490. err = copy_gathers(host->dev, job, dev);
  491. if (err)
  492. goto out;
  493. }
  494. /* patch gathers */
  495. for (i = 0; i < job->num_cmds; i++) {
  496. struct host1x_job_gather *g;
  497. if (job->cmds[i].is_wait)
  498. continue;
  499. g = &job->cmds[i].gather;
  500. /* process each gather mem only once */
  501. if (g->handled)
  502. continue;
  503. /* copy_gathers() sets gathers base if firewall is enabled */
  504. if (!job->enable_firewall)
  505. g->base = job->gather_addr_phys[i];
  506. for (j = i + 1; j < job->num_cmds; j++) {
  507. if (!job->cmds[j].is_wait &&
  508. job->cmds[j].gather.bo == g->bo) {
  509. job->cmds[j].gather.handled = true;
  510. job->cmds[j].gather.base = g->base;
  511. }
  512. }
  513. err = do_relocs(job, g);
  514. if (err)
  515. break;
  516. }
  517. out:
  518. if (err)
  519. host1x_job_unpin(job);
  520. wmb();
  521. return err;
  522. }
  523. EXPORT_SYMBOL(host1x_job_pin);
  524. void host1x_job_unpin(struct host1x_job *job)
  525. {
  526. struct host1x *host = dev_get_drvdata(job->channel->dev->parent);
  527. unsigned int i;
  528. for (i = 0; i < job->num_unpins; i++) {
  529. struct host1x_bo_mapping *map = job->unpins[i].map;
  530. struct host1x_bo *bo = map->bo;
  531. if (!job->enable_firewall && map->size && host->domain) {
  532. iommu_unmap(host->domain, job->addr_phys[i], map->size);
  533. free_iova(&host->iova, iova_pfn(&host->iova, job->addr_phys[i]));
  534. }
  535. host1x_bo_unpin(map);
  536. host1x_bo_put(bo);
  537. }
  538. job->num_unpins = 0;
  539. if (job->gather_copy_size)
  540. dma_free_wc(host->dev, job->gather_copy_size,
  541. job->gather_copy_mapped, job->gather_copy);
  542. }
  543. EXPORT_SYMBOL(host1x_job_unpin);
  544. /*
  545. * Debug routine used to dump job entries
  546. */
  547. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  548. {
  549. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt->id);
  550. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  551. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  552. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  553. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  554. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  555. }