job.c 13 KB

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