job.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. /*
  2. * Tegra host1x Job
  3. *
  4. * Copyright (c) 2010-2013, NVIDIA Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/host1x.h>
  21. #include <linux/kref.h>
  22. #include <linux/module.h>
  23. #include <linux/scatterlist.h>
  24. #include <linux/slab.h>
  25. #include <linux/vmalloc.h>
  26. #include <trace/events/host1x.h>
  27. #include "channel.h"
  28. #include "dev.h"
  29. #include "job.h"
  30. #include "syncpt.h"
  31. struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
  32. u32 num_cmdbufs, u32 num_relocs,
  33. u32 num_waitchks)
  34. {
  35. struct host1x_job *job = NULL;
  36. unsigned int num_unpins = num_cmdbufs + num_relocs;
  37. u64 total;
  38. void *mem;
  39. /* Check that we're not going to overflow */
  40. total = sizeof(struct host1x_job) +
  41. (u64)num_relocs * sizeof(struct host1x_reloc) +
  42. (u64)num_unpins * sizeof(struct host1x_job_unpin_data) +
  43. (u64)num_waitchks * sizeof(struct host1x_waitchk) +
  44. (u64)num_cmdbufs * sizeof(struct host1x_job_gather) +
  45. (u64)num_unpins * sizeof(dma_addr_t) +
  46. (u64)num_unpins * sizeof(u32 *);
  47. if (total > ULONG_MAX)
  48. return NULL;
  49. mem = job = kzalloc(total, GFP_KERNEL);
  50. if (!job)
  51. return NULL;
  52. kref_init(&job->ref);
  53. job->channel = ch;
  54. /* Redistribute memory to the structs */
  55. mem += sizeof(struct host1x_job);
  56. job->relocarray = num_relocs ? mem : NULL;
  57. mem += num_relocs * sizeof(struct host1x_reloc);
  58. job->unpins = num_unpins ? mem : NULL;
  59. mem += num_unpins * sizeof(struct host1x_job_unpin_data);
  60. job->waitchk = num_waitchks ? mem : NULL;
  61. mem += num_waitchks * sizeof(struct host1x_waitchk);
  62. job->gathers = num_cmdbufs ? mem : NULL;
  63. mem += num_cmdbufs * sizeof(struct host1x_job_gather);
  64. job->addr_phys = num_unpins ? mem : NULL;
  65. job->reloc_addr_phys = job->addr_phys;
  66. job->gather_addr_phys = &job->addr_phys[num_relocs];
  67. return job;
  68. }
  69. EXPORT_SYMBOL(host1x_job_alloc);
  70. struct host1x_job *host1x_job_get(struct host1x_job *job)
  71. {
  72. kref_get(&job->ref);
  73. return job;
  74. }
  75. EXPORT_SYMBOL(host1x_job_get);
  76. static void job_free(struct kref *ref)
  77. {
  78. struct host1x_job *job = container_of(ref, struct host1x_job, ref);
  79. kfree(job);
  80. }
  81. void host1x_job_put(struct host1x_job *job)
  82. {
  83. kref_put(&job->ref, job_free);
  84. }
  85. EXPORT_SYMBOL(host1x_job_put);
  86. void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
  87. u32 words, u32 offset)
  88. {
  89. struct host1x_job_gather *cur_gather = &job->gathers[job->num_gathers];
  90. cur_gather->words = words;
  91. cur_gather->bo = bo;
  92. cur_gather->offset = offset;
  93. job->num_gathers++;
  94. }
  95. EXPORT_SYMBOL(host1x_job_add_gather);
  96. /*
  97. * NULL an already satisfied WAIT_SYNCPT host method, by patching its
  98. * args in the command stream. The method data is changed to reference
  99. * a reserved (never given out or incr) HOST1X_SYNCPT_RESERVED syncpt
  100. * with a matching threshold value of 0, so is guaranteed to be popped
  101. * by the host HW.
  102. */
  103. static void host1x_syncpt_patch_offset(struct host1x_syncpt *sp,
  104. struct host1x_bo *h, u32 offset)
  105. {
  106. void *patch_addr = NULL;
  107. /* patch the wait */
  108. patch_addr = host1x_bo_kmap(h, offset >> PAGE_SHIFT);
  109. if (patch_addr) {
  110. host1x_syncpt_patch_wait(sp,
  111. patch_addr + (offset & ~PAGE_MASK));
  112. host1x_bo_kunmap(h, offset >> PAGE_SHIFT, patch_addr);
  113. } else
  114. pr_err("Could not map cmdbuf for wait check\n");
  115. }
  116. /*
  117. * Check driver supplied waitchk structs for syncpt thresholds
  118. * that have already been satisfied and NULL the comparison (to
  119. * avoid a wrap condition in the HW).
  120. */
  121. static int do_waitchks(struct host1x_job *job, struct host1x *host,
  122. struct host1x_bo *patch)
  123. {
  124. int i;
  125. /* compare syncpt vs wait threshold */
  126. for (i = 0; i < job->num_waitchk; i++) {
  127. struct host1x_waitchk *wait = &job->waitchk[i];
  128. struct host1x_syncpt *sp =
  129. host1x_syncpt_get(host, wait->syncpt_id);
  130. /* validate syncpt id */
  131. if (wait->syncpt_id > host1x_syncpt_nb_pts(host))
  132. continue;
  133. /* skip all other gathers */
  134. if (patch != wait->bo)
  135. continue;
  136. trace_host1x_syncpt_wait_check(wait->bo, wait->offset,
  137. wait->syncpt_id, wait->thresh,
  138. host1x_syncpt_read_min(sp));
  139. if (host1x_syncpt_is_expired(sp, wait->thresh)) {
  140. dev_dbg(host->dev,
  141. "drop WAIT id %d (%s) thresh 0x%x, min 0x%x\n",
  142. wait->syncpt_id, sp->name, wait->thresh,
  143. host1x_syncpt_read_min(sp));
  144. host1x_syncpt_patch_offset(sp, patch, wait->offset);
  145. }
  146. wait->bo = NULL;
  147. }
  148. return 0;
  149. }
  150. static unsigned int pin_job(struct host1x_job *job)
  151. {
  152. unsigned int i;
  153. job->num_unpins = 0;
  154. for (i = 0; i < job->num_relocs; i++) {
  155. struct host1x_reloc *reloc = &job->relocarray[i];
  156. struct sg_table *sgt;
  157. dma_addr_t phys_addr;
  158. reloc->target.bo = host1x_bo_get(reloc->target.bo);
  159. if (!reloc->target.bo)
  160. goto unpin;
  161. phys_addr = host1x_bo_pin(reloc->target.bo, &sgt);
  162. if (!phys_addr)
  163. goto unpin;
  164. job->addr_phys[job->num_unpins] = phys_addr;
  165. job->unpins[job->num_unpins].bo = reloc->target.bo;
  166. job->unpins[job->num_unpins].sgt = sgt;
  167. job->num_unpins++;
  168. }
  169. for (i = 0; i < job->num_gathers; i++) {
  170. struct host1x_job_gather *g = &job->gathers[i];
  171. struct sg_table *sgt;
  172. dma_addr_t phys_addr;
  173. g->bo = host1x_bo_get(g->bo);
  174. if (!g->bo)
  175. goto unpin;
  176. phys_addr = host1x_bo_pin(g->bo, &sgt);
  177. if (!phys_addr)
  178. goto unpin;
  179. job->addr_phys[job->num_unpins] = phys_addr;
  180. job->unpins[job->num_unpins].bo = g->bo;
  181. job->unpins[job->num_unpins].sgt = sgt;
  182. job->num_unpins++;
  183. }
  184. return job->num_unpins;
  185. unpin:
  186. host1x_job_unpin(job);
  187. return 0;
  188. }
  189. static unsigned int do_relocs(struct host1x_job *job, struct host1x_bo *cmdbuf)
  190. {
  191. int i = 0;
  192. u32 last_page = ~0;
  193. void *cmdbuf_page_addr = NULL;
  194. /* pin & patch the relocs for one gather */
  195. for (i = 0; i < job->num_relocs; i++) {
  196. struct host1x_reloc *reloc = &job->relocarray[i];
  197. u32 reloc_addr = (job->reloc_addr_phys[i] +
  198. reloc->target.offset) >> reloc->shift;
  199. u32 *target;
  200. /* skip all other gathers */
  201. if (cmdbuf != reloc->cmdbuf.bo)
  202. continue;
  203. if (last_page != reloc->cmdbuf.offset >> PAGE_SHIFT) {
  204. if (cmdbuf_page_addr)
  205. host1x_bo_kunmap(cmdbuf, last_page,
  206. cmdbuf_page_addr);
  207. cmdbuf_page_addr = host1x_bo_kmap(cmdbuf,
  208. reloc->cmdbuf.offset >> PAGE_SHIFT);
  209. last_page = reloc->cmdbuf.offset >> PAGE_SHIFT;
  210. if (unlikely(!cmdbuf_page_addr)) {
  211. pr_err("Could not map cmdbuf for relocation\n");
  212. return -ENOMEM;
  213. }
  214. }
  215. target = cmdbuf_page_addr + (reloc->cmdbuf.offset & ~PAGE_MASK);
  216. *target = reloc_addr;
  217. }
  218. if (cmdbuf_page_addr)
  219. host1x_bo_kunmap(cmdbuf, last_page, cmdbuf_page_addr);
  220. return 0;
  221. }
  222. static bool check_reloc(struct host1x_reloc *reloc, struct host1x_bo *cmdbuf,
  223. unsigned int offset)
  224. {
  225. offset *= sizeof(u32);
  226. if (reloc->cmdbuf.bo != cmdbuf || reloc->cmdbuf.offset != offset)
  227. return false;
  228. return true;
  229. }
  230. struct host1x_firewall {
  231. struct host1x_job *job;
  232. struct device *dev;
  233. unsigned int num_relocs;
  234. struct host1x_reloc *reloc;
  235. struct host1x_bo *cmdbuf;
  236. unsigned int offset;
  237. u32 words;
  238. u32 class;
  239. u32 reg;
  240. u32 mask;
  241. u32 count;
  242. };
  243. static int check_register(struct host1x_firewall *fw, unsigned long offset)
  244. {
  245. if (fw->job->is_addr_reg(fw->dev, fw->class, offset)) {
  246. if (!fw->num_relocs)
  247. return -EINVAL;
  248. if (!check_reloc(fw->reloc, fw->cmdbuf, fw->offset))
  249. return -EINVAL;
  250. fw->num_relocs--;
  251. fw->reloc++;
  252. }
  253. return 0;
  254. }
  255. static int check_mask(struct host1x_firewall *fw)
  256. {
  257. u32 mask = fw->mask;
  258. u32 reg = fw->reg;
  259. int ret;
  260. while (mask) {
  261. if (fw->words == 0)
  262. return -EINVAL;
  263. if (mask & 1) {
  264. ret = check_register(fw, reg);
  265. if (ret < 0)
  266. return ret;
  267. fw->words--;
  268. fw->offset++;
  269. }
  270. mask >>= 1;
  271. reg++;
  272. }
  273. return 0;
  274. }
  275. static int check_incr(struct host1x_firewall *fw)
  276. {
  277. u32 count = fw->count;
  278. u32 reg = fw->reg;
  279. int ret;
  280. while (count) {
  281. if (fw->words == 0)
  282. return -EINVAL;
  283. ret = check_register(fw, reg);
  284. if (ret < 0)
  285. return ret;
  286. reg++;
  287. fw->words--;
  288. fw->offset++;
  289. count--;
  290. }
  291. return 0;
  292. }
  293. static int check_nonincr(struct host1x_firewall *fw)
  294. {
  295. u32 count = fw->count;
  296. int ret;
  297. while (count) {
  298. if (fw->words == 0)
  299. return -EINVAL;
  300. ret = check_register(fw, fw->reg);
  301. if (ret < 0)
  302. return ret;
  303. fw->words--;
  304. fw->offset++;
  305. count--;
  306. }
  307. return 0;
  308. }
  309. static int validate(struct host1x_firewall *fw, struct host1x_job_gather *g)
  310. {
  311. u32 *cmdbuf_base = (u32 *)fw->job->gather_copy_mapped +
  312. (g->offset / sizeof(u32));
  313. int err = 0;
  314. if (!fw->job->is_addr_reg)
  315. return 0;
  316. fw->words = g->words;
  317. fw->cmdbuf = g->bo;
  318. fw->offset = 0;
  319. while (fw->words && !err) {
  320. u32 word = cmdbuf_base[fw->offset];
  321. u32 opcode = (word & 0xf0000000) >> 28;
  322. fw->mask = 0;
  323. fw->reg = 0;
  324. fw->count = 0;
  325. fw->words--;
  326. fw->offset++;
  327. switch (opcode) {
  328. case 0:
  329. fw->class = word >> 6 & 0x3ff;
  330. fw->mask = word & 0x3f;
  331. fw->reg = word >> 16 & 0xfff;
  332. err = check_mask(fw);
  333. if (err)
  334. goto out;
  335. break;
  336. case 1:
  337. fw->reg = word >> 16 & 0xfff;
  338. fw->count = word & 0xffff;
  339. err = check_incr(fw);
  340. if (err)
  341. goto out;
  342. break;
  343. case 2:
  344. fw->reg = word >> 16 & 0xfff;
  345. fw->count = word & 0xffff;
  346. err = check_nonincr(fw);
  347. if (err)
  348. goto out;
  349. break;
  350. case 3:
  351. fw->mask = word & 0xffff;
  352. fw->reg = word >> 16 & 0xfff;
  353. err = check_mask(fw);
  354. if (err)
  355. goto out;
  356. break;
  357. case 4:
  358. case 5:
  359. case 14:
  360. break;
  361. default:
  362. err = -EINVAL;
  363. break;
  364. }
  365. }
  366. out:
  367. return err;
  368. }
  369. static inline int copy_gathers(struct host1x_job *job, struct device *dev)
  370. {
  371. struct host1x_firewall fw;
  372. size_t size = 0;
  373. size_t offset = 0;
  374. int i;
  375. fw.job = job;
  376. fw.dev = dev;
  377. fw.reloc = job->relocarray;
  378. fw.num_relocs = job->num_relocs;
  379. fw.class = 0;
  380. for (i = 0; i < job->num_gathers; i++) {
  381. struct host1x_job_gather *g = &job->gathers[i];
  382. size += g->words * sizeof(u32);
  383. }
  384. job->gather_copy_mapped = dma_alloc_writecombine(dev, size,
  385. &job->gather_copy,
  386. GFP_KERNEL);
  387. if (!job->gather_copy_mapped) {
  388. job->gather_copy_mapped = NULL;
  389. return -ENOMEM;
  390. }
  391. job->gather_copy_size = size;
  392. for (i = 0; i < job->num_gathers; i++) {
  393. struct host1x_job_gather *g = &job->gathers[i];
  394. void *gather;
  395. /* Copy the gather */
  396. gather = host1x_bo_mmap(g->bo);
  397. memcpy(job->gather_copy_mapped + offset, gather + g->offset,
  398. g->words * sizeof(u32));
  399. host1x_bo_munmap(g->bo, gather);
  400. /* Store the location in the buffer */
  401. g->base = job->gather_copy;
  402. g->offset = offset;
  403. /* Validate the job */
  404. if (validate(&fw, g))
  405. return -EINVAL;
  406. offset += g->words * sizeof(u32);
  407. }
  408. /* No relocs should remain at this point */
  409. if (fw.num_relocs)
  410. return -EINVAL;
  411. return 0;
  412. }
  413. int host1x_job_pin(struct host1x_job *job, struct device *dev)
  414. {
  415. int err;
  416. unsigned int i, j;
  417. struct host1x *host = dev_get_drvdata(dev->parent);
  418. DECLARE_BITMAP(waitchk_mask, host1x_syncpt_nb_pts(host));
  419. bitmap_zero(waitchk_mask, host1x_syncpt_nb_pts(host));
  420. for (i = 0; i < job->num_waitchk; i++) {
  421. u32 syncpt_id = job->waitchk[i].syncpt_id;
  422. if (syncpt_id < host1x_syncpt_nb_pts(host))
  423. set_bit(syncpt_id, waitchk_mask);
  424. }
  425. /* get current syncpt values for waitchk */
  426. for_each_set_bit(i, waitchk_mask, host1x_syncpt_nb_pts(host))
  427. host1x_syncpt_load(host->syncpt + i);
  428. /* pin memory */
  429. err = pin_job(job);
  430. if (!err)
  431. goto out;
  432. /* patch gathers */
  433. for (i = 0; i < job->num_gathers; i++) {
  434. struct host1x_job_gather *g = &job->gathers[i];
  435. /* process each gather mem only once */
  436. if (g->handled)
  437. continue;
  438. g->base = job->gather_addr_phys[i];
  439. for (j = i + 1; j < job->num_gathers; j++)
  440. if (job->gathers[j].bo == g->bo)
  441. job->gathers[j].handled = true;
  442. err = do_relocs(job, g->bo);
  443. if (err)
  444. break;
  445. err = do_waitchks(job, host, g->bo);
  446. if (err)
  447. break;
  448. }
  449. if (IS_ENABLED(CONFIG_TEGRA_HOST1X_FIREWALL) && !err) {
  450. err = copy_gathers(job, dev);
  451. if (err) {
  452. host1x_job_unpin(job);
  453. return err;
  454. }
  455. }
  456. out:
  457. wmb();
  458. return err;
  459. }
  460. EXPORT_SYMBOL(host1x_job_pin);
  461. void host1x_job_unpin(struct host1x_job *job)
  462. {
  463. unsigned int i;
  464. for (i = 0; i < job->num_unpins; i++) {
  465. struct host1x_job_unpin_data *unpin = &job->unpins[i];
  466. host1x_bo_unpin(unpin->bo, unpin->sgt);
  467. host1x_bo_put(unpin->bo);
  468. }
  469. job->num_unpins = 0;
  470. if (job->gather_copy_size)
  471. dma_free_writecombine(job->channel->dev, job->gather_copy_size,
  472. job->gather_copy_mapped,
  473. job->gather_copy);
  474. }
  475. EXPORT_SYMBOL(host1x_job_unpin);
  476. /*
  477. * Debug routine used to dump job entries
  478. */
  479. void host1x_job_dump(struct device *dev, struct host1x_job *job)
  480. {
  481. dev_dbg(dev, " SYNCPT_ID %d\n", job->syncpt_id);
  482. dev_dbg(dev, " SYNCPT_VAL %d\n", job->syncpt_end);
  483. dev_dbg(dev, " FIRST_GET 0x%x\n", job->first_get);
  484. dev_dbg(dev, " TIMEOUT %d\n", job->timeout);
  485. dev_dbg(dev, " NUM_SLOTS %d\n", job->num_slots);
  486. dev_dbg(dev, " NUM_HANDLES %d\n", job->num_unpins);
  487. }