nouveau_compat.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. #include "nouveau_drm.h"
  2. #include "nouveau_chan.h"
  3. #include "nouveau_compat.h"
  4. #include <subdev/bios.h>
  5. #include <subdev/bios/dcb.h>
  6. #include <subdev/bios/init.h>
  7. #include <subdev/bios/pll.h>
  8. #include <subdev/gpio.h>
  9. #include <subdev/i2c.h>
  10. #include <subdev/clock.h>
  11. #include <subdev/mc.h>
  12. #include <subdev/timer.h>
  13. #include <subdev/fb.h>
  14. #include <subdev/bar.h>
  15. #include <subdev/vm.h>
  16. int
  17. nvdrm_gart_init(struct drm_device *dev, u64 *base, u64 *size)
  18. {
  19. struct nouveau_drm *drm = nouveau_newpriv(dev);
  20. if (drm->agp.stat == ENABLED) {
  21. *base = drm->agp.base;
  22. *size = drm->agp.base;
  23. return 0;
  24. }
  25. return -ENODEV;
  26. }
  27. u8
  28. _nv_rd08(struct drm_device *dev, u32 reg)
  29. {
  30. struct nouveau_drm *drm = nouveau_newpriv(dev);
  31. return nv_ro08(drm->device, reg);
  32. }
  33. void
  34. _nv_wr08(struct drm_device *dev, u32 reg, u8 val)
  35. {
  36. struct nouveau_drm *drm = nouveau_newpriv(dev);
  37. nv_wo08(drm->device, reg, val);
  38. }
  39. u32
  40. _nv_rd32(struct drm_device *dev, u32 reg)
  41. {
  42. struct nouveau_drm *drm = nouveau_newpriv(dev);
  43. return nv_ro32(drm->device, reg);
  44. }
  45. void
  46. _nv_wr32(struct drm_device *dev, u32 reg, u32 val)
  47. {
  48. struct nouveau_drm *drm = nouveau_newpriv(dev);
  49. nv_wo32(drm->device, reg, val);
  50. }
  51. u32
  52. _nv_mask(struct drm_device *dev, u32 reg, u32 mask, u32 val)
  53. {
  54. u32 tmp = _nv_rd32(dev, reg);
  55. _nv_wr32(dev, reg, (tmp & ~mask) | val);
  56. return tmp;
  57. }
  58. bool
  59. _nv_bios(struct drm_device *dev, u8 **data, u32 *size)
  60. {
  61. struct nouveau_drm *drm = nouveau_newpriv(dev);
  62. struct nouveau_bios *bios = nouveau_bios(drm->device);
  63. *data = bios->data;
  64. *size = bios->size;
  65. return true;
  66. }
  67. void
  68. nouveau_gpio_reset(struct drm_device *dev)
  69. {
  70. struct nouveau_drm *drm = nouveau_newpriv(dev);
  71. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  72. gpio->reset(gpio);
  73. }
  74. int
  75. nouveau_gpio_find(struct drm_device *dev, int idx, u8 tag, u8 line,
  76. struct dcb_gpio_func *func)
  77. {
  78. struct nouveau_drm *drm = nouveau_newpriv(dev);
  79. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  80. return gpio->find(gpio, idx, tag, line, func);
  81. }
  82. bool
  83. nouveau_gpio_func_valid(struct drm_device *dev, u8 tag)
  84. {
  85. struct nouveau_drm *drm = nouveau_newpriv(dev);
  86. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  87. struct dcb_gpio_func func;
  88. return gpio->find(gpio, 0, tag, 0xff, &func) == 0;
  89. }
  90. int
  91. nouveau_gpio_func_set(struct drm_device *dev, u8 tag, int state)
  92. {
  93. struct nouveau_drm *drm = nouveau_newpriv(dev);
  94. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  95. if (gpio && gpio->get)
  96. return gpio->set(gpio, 0, tag, 0xff, state);
  97. return -ENODEV;
  98. }
  99. int
  100. nouveau_gpio_func_get(struct drm_device *dev, u8 tag)
  101. {
  102. struct nouveau_drm *drm = nouveau_newpriv(dev);
  103. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  104. if (gpio && gpio->get)
  105. return gpio->get(gpio, 0, tag, 0xff);
  106. return -ENODEV;
  107. }
  108. int
  109. nouveau_gpio_irq(struct drm_device *dev, int idx, u8 tag, u8 line, bool on)
  110. {
  111. struct nouveau_drm *drm = nouveau_newpriv(dev);
  112. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  113. if (gpio && gpio->irq)
  114. return gpio->irq(gpio, idx, tag, line, on);
  115. return -ENODEV;
  116. }
  117. int
  118. nouveau_gpio_isr_add(struct drm_device *dev, int idx, u8 tag, u8 line,
  119. void (*exec)(void *, int state), void *data)
  120. {
  121. struct nouveau_drm *drm = nouveau_newpriv(dev);
  122. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  123. if (gpio && gpio->isr_add)
  124. return gpio->isr_add(gpio, idx, tag, line, exec, data);
  125. return -ENODEV;
  126. }
  127. void
  128. nouveau_gpio_isr_del(struct drm_device *dev, int idx, u8 tag, u8 line,
  129. void (*exec)(void *, int state), void *data)
  130. {
  131. struct nouveau_drm *drm = nouveau_newpriv(dev);
  132. struct nouveau_gpio *gpio = nouveau_gpio(drm->device);
  133. if (gpio && gpio->isr_del)
  134. gpio->isr_del(gpio, idx, tag, line, exec, data);
  135. }
  136. struct nouveau_i2c_port *
  137. nouveau_i2c_find(struct drm_device *dev, u8 index)
  138. {
  139. struct nouveau_drm *drm = nouveau_newpriv(dev);
  140. struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
  141. return i2c->find(i2c, index);
  142. }
  143. bool
  144. nouveau_probe_i2c_addr(struct nouveau_i2c_port *port, int addr)
  145. {
  146. return nv_probe_i2c(port, addr);
  147. }
  148. struct i2c_adapter *
  149. nouveau_i2c_adapter(struct nouveau_i2c_port *port)
  150. {
  151. return &port->adapter;
  152. }
  153. int
  154. nouveau_i2c_identify(struct drm_device *dev, const char *what,
  155. struct i2c_board_info *info,
  156. bool (*match)(struct nouveau_i2c_port *,
  157. struct i2c_board_info *),
  158. int index)
  159. {
  160. struct nouveau_drm *drm = nouveau_newpriv(dev);
  161. struct nouveau_i2c *i2c = nouveau_i2c(drm->device);
  162. return i2c->identify(i2c, index, what, info, match);
  163. }
  164. int
  165. auxch_rd(struct drm_device *dev, struct nouveau_i2c_port *port,
  166. u32 addr, u8 *data, u8 size)
  167. {
  168. return nv_rdaux(port, addr, data, size);
  169. }
  170. int
  171. auxch_wr(struct drm_device *dev, struct nouveau_i2c_port *port,
  172. u32 addr, u8 *data, u8 size)
  173. {
  174. return nv_wraux(port, addr, data, size);
  175. }
  176. u32
  177. get_pll_register(struct drm_device *dev, u32 type)
  178. {
  179. struct nouveau_drm *drm = nouveau_newpriv(dev);
  180. struct nouveau_bios *bios = nouveau_bios(drm->device);
  181. struct nvbios_pll info;
  182. if (nvbios_pll_parse(bios, type, &info))
  183. return 0;
  184. return info.reg;
  185. }
  186. int
  187. get_pll_limits(struct drm_device *dev, u32 type, struct nvbios_pll *info)
  188. {
  189. struct nouveau_drm *drm = nouveau_newpriv(dev);
  190. struct nouveau_bios *bios = nouveau_bios(drm->device);
  191. return nvbios_pll_parse(bios, type, info);
  192. }
  193. int
  194. setPLL(struct drm_device *dev, u32 reg, u32 freq)
  195. {
  196. struct nouveau_drm *drm = nouveau_newpriv(dev);
  197. struct nouveau_clock *clk = nouveau_clock(drm->device);
  198. int ret = -ENODEV;
  199. if (clk->pll_set)
  200. ret = clk->pll_set(clk, reg, freq);
  201. return ret;
  202. }
  203. int
  204. nouveau_calc_pll_mnp(struct drm_device *dev, struct nvbios_pll *info,
  205. int freq, struct nouveau_pll_vals *pv)
  206. {
  207. struct nouveau_drm *drm = nouveau_newpriv(dev);
  208. struct nouveau_clock *clk = nouveau_clock(drm->device);
  209. int ret = 0;
  210. if (clk->pll_calc)
  211. ret = clk->pll_calc(clk, info, freq, pv);
  212. return ret;
  213. }
  214. int
  215. nouveau_hw_setpll(struct drm_device *dev, u32 reg1,
  216. struct nouveau_pll_vals *pv)
  217. {
  218. struct nouveau_drm *drm = nouveau_newpriv(dev);
  219. struct nouveau_clock *clk = nouveau_clock(drm->device);
  220. int ret = -ENODEV;
  221. if (clk->pll_prog)
  222. ret = clk->pll_prog(clk, reg1, pv);
  223. return ret;
  224. }
  225. int nva3_pll_calc(struct nouveau_clock *, struct nvbios_pll *, u32 freq,
  226. int *N, int *fN, int *M, int *P);
  227. int
  228. nva3_calc_pll(struct drm_device *dev, struct nvbios_pll *info, u32 freq,
  229. int *N, int *fN, int *M, int *P)
  230. {
  231. struct nouveau_drm *drm = nouveau_newpriv(dev);
  232. struct nouveau_clock *clk = nouveau_clock(drm->device);
  233. return nva3_pll_calc(clk, info, freq, N, fN, M, P);
  234. }
  235. void
  236. nouveau_bios_run_init_table(struct drm_device *dev, uint16_t table,
  237. struct dcb_output *dcbent, int crtc)
  238. {
  239. struct nouveau_drm *drm = nouveau_newpriv(dev);
  240. struct nouveau_bios *bios = nouveau_bios(drm->device);
  241. struct nvbios_init init = {
  242. .subdev = nv_subdev(bios),
  243. .bios = bios,
  244. .offset = table,
  245. .outp = dcbent,
  246. .crtc = crtc,
  247. .execute = 1
  248. };
  249. nvbios_exec(&init);
  250. }
  251. void
  252. nouveau_bios_init_exec(struct drm_device *dev, uint16_t table)
  253. {
  254. nouveau_bios_run_init_table(dev, table, NULL, 0);
  255. }
  256. void
  257. nv_intr(struct drm_device *dev)
  258. {
  259. struct nouveau_drm *drm = nouveau_newpriv(dev);
  260. struct nouveau_mc *pmc = nouveau_mc(drm->device);
  261. nv_subdev(pmc)->intr(&pmc->base);
  262. }
  263. bool nouveau_wait_eq(struct drm_device *dev, uint64_t timeout,
  264. uint32_t reg, uint32_t mask, uint32_t val)
  265. {
  266. struct nouveau_drm *drm = nouveau_newpriv(dev);
  267. return nouveau_timer_wait_eq(drm->device, timeout, reg, mask, val);
  268. }
  269. bool nouveau_wait_ne(struct drm_device *dev, uint64_t timeout,
  270. uint32_t reg, uint32_t mask, uint32_t val)
  271. {
  272. struct nouveau_drm *drm = nouveau_newpriv(dev);
  273. return nouveau_timer_wait_ne(drm->device, timeout, reg, mask, val);
  274. }
  275. bool nouveau_wait_cb(struct drm_device *dev, u64 timeout,
  276. bool (*cond)(void *), void *data)
  277. {
  278. struct nouveau_drm *drm = nouveau_newpriv(dev);
  279. return nouveau_timer_wait_cb(drm->device, timeout, cond, data);
  280. }
  281. u64
  282. nv_timer_read(struct drm_device *dev)
  283. {
  284. struct nouveau_drm *drm = nouveau_newpriv(dev);
  285. struct nouveau_timer *ptimer = nouveau_timer(drm->device);
  286. return ptimer->read(ptimer);
  287. }
  288. int
  289. nvfb_tile_nr(struct drm_device *dev)
  290. {
  291. struct nouveau_drm *drm = nouveau_newpriv(dev);
  292. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  293. return pfb->tile.regions;
  294. }
  295. struct nouveau_fb_tile *
  296. nvfb_tile(struct drm_device *dev, int i)
  297. {
  298. struct nouveau_drm *drm = nouveau_newpriv(dev);
  299. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  300. return &pfb->tile.region[i];
  301. }
  302. void
  303. nvfb_tile_init(struct drm_device *dev, int i, u32 a, u32 b, u32 c, u32 d)
  304. {
  305. struct nouveau_drm *drm = nouveau_newpriv(dev);
  306. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  307. pfb->tile.init(pfb, i, a, b, c, d, &pfb->tile.region[i]);
  308. }
  309. void
  310. nvfb_tile_fini(struct drm_device *dev, int i)
  311. {
  312. struct nouveau_drm *drm = nouveau_newpriv(dev);
  313. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  314. pfb->tile.fini(pfb, i, &pfb->tile.region[i]);
  315. }
  316. void
  317. nvfb_tile_prog(struct drm_device *dev, int i)
  318. {
  319. struct nouveau_drm *drm = nouveau_newpriv(dev);
  320. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  321. pfb->tile.prog(pfb, i, &pfb->tile.region[i]);
  322. }
  323. bool
  324. nvfb_flags_valid(struct drm_device *dev, u32 flags)
  325. {
  326. struct nouveau_drm *drm = nouveau_newpriv(dev);
  327. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  328. return pfb->memtype_valid(pfb, flags);
  329. }
  330. int
  331. nvfb_vram_get(struct drm_device *dev, u64 size, u32 align, u32 ncmin,
  332. u32 memtype, struct nouveau_mem **pmem)
  333. {
  334. struct nouveau_drm *drm = nouveau_newpriv(dev);
  335. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  336. int ret = pfb->ram.get(pfb, size, align, ncmin, memtype, pmem);
  337. if (ret)
  338. return ret;
  339. (*pmem)->dev = dev;
  340. return 0;
  341. }
  342. void
  343. nvfb_vram_put(struct drm_device *dev, struct nouveau_mem **pmem)
  344. {
  345. struct nouveau_drm *drm = nouveau_newpriv(dev);
  346. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  347. pfb->ram.put(pfb, pmem);
  348. }
  349. u64 nvfb_vram_sys_base(struct drm_device *dev)
  350. {
  351. struct nouveau_drm *drm = nouveau_newpriv(dev);
  352. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  353. return pfb->ram.stolen;
  354. }
  355. u64 nvfb_vram_size(struct drm_device *dev)
  356. {
  357. struct nouveau_drm *drm = nouveau_newpriv(dev);
  358. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  359. return pfb->ram.size;
  360. }
  361. int nvfb_vram_type(struct drm_device *dev)
  362. {
  363. struct nouveau_drm *drm = nouveau_newpriv(dev);
  364. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  365. return pfb->ram.type;
  366. }
  367. int nvfb_vram_rank_B(struct drm_device *dev)
  368. {
  369. struct nouveau_drm *drm = nouveau_newpriv(dev);
  370. struct nouveau_fb *pfb = nouveau_fb(drm->device);
  371. return pfb->ram.ranks > 1;
  372. }
  373. void
  374. nv50_fb_vm_trap(struct drm_device *dev, int disp)
  375. {
  376. struct nouveau_drm *drm = nouveau_newpriv(dev);
  377. nv50_fb_trap(nouveau_fb(drm->device), disp);
  378. }
  379. #include <core/subdev/instmem/nv04.h>
  380. struct nouveau_gpuobj *
  381. nvimem_ramro(struct drm_device *dev)
  382. {
  383. struct nouveau_drm *drm = nouveau_newpriv(dev);
  384. struct nv04_instmem_priv *imem = (void *)nouveau_instmem(drm->device);
  385. return imem->ramro;
  386. }
  387. struct nouveau_gpuobj *
  388. nvimem_ramfc(struct drm_device *dev)
  389. {
  390. struct nouveau_drm *drm = nouveau_newpriv(dev);
  391. struct nv04_instmem_priv *imem = (void *)nouveau_instmem(drm->device);
  392. return imem->ramfc;
  393. }
  394. int _nouveau_gpuobj_new(struct drm_device *dev, struct nouveau_gpuobj *par,
  395. int size, int align, u32 flags,
  396. struct nouveau_gpuobj **pobj)
  397. {
  398. struct nouveau_drm *drm = nouveau_newpriv(dev);
  399. int ret;
  400. if (!par)
  401. flags |= NVOBJ_FLAG_HEAP;
  402. ret = nouveau_gpuobj_new(drm->device, nv_object(par), size, align,
  403. flags, pobj);
  404. if (ret)
  405. return ret;
  406. (*pobj)->dev = dev;
  407. return 0;
  408. }
  409. u32 nv_ri32(struct drm_device *dev , u32 addr)
  410. {
  411. struct nouveau_drm *drm = nouveau_newpriv(dev);
  412. struct nouveau_instmem *imem = nouveau_instmem(drm->device);
  413. return nv_ro32(imem, addr);
  414. }
  415. void nv_wi32(struct drm_device *dev, u32 addr, u32 data)
  416. {
  417. struct nouveau_drm *drm = nouveau_newpriv(dev);
  418. struct nouveau_instmem *imem = nouveau_instmem(drm->device);
  419. nv_wo32(imem, addr, data);
  420. }
  421. u32 nvimem_reserved(struct drm_device *dev)
  422. {
  423. struct nouveau_drm *drm = nouveau_newpriv(dev);
  424. struct nouveau_instmem *imem = nouveau_instmem(drm->device);
  425. return imem->reserved;
  426. }
  427. int
  428. nvbar_map(struct drm_device *dev, struct nouveau_mem *mem, u32 flags,
  429. struct nouveau_vma *vma)
  430. {
  431. struct nouveau_drm *drm = nouveau_newpriv(dev);
  432. struct nouveau_bar *bar = nouveau_bar(drm->device);
  433. return bar->umap(bar, mem, flags, vma);
  434. }
  435. void
  436. nvbar_unmap(struct drm_device *dev, struct nouveau_vma *vma)
  437. {
  438. struct nouveau_drm *drm = nouveau_newpriv(dev);
  439. struct nouveau_bar *bar = nouveau_bar(drm->device);
  440. bar->unmap(bar, vma);
  441. }
  442. int
  443. nouveau_gpuobj_map_bar(struct nouveau_gpuobj *gpuobj, u32 flags,
  444. struct nouveau_vma *vma)
  445. {
  446. struct nouveau_drm *drm = nouveau_newpriv(gpuobj->dev);
  447. struct nouveau_bar *bar = nouveau_bar(drm->device);
  448. struct nouveau_instobj *iobj = (void *)
  449. nv_pclass(nv_object(gpuobj), NV_MEMOBJ_CLASS);
  450. struct nouveau_mem **mem = (void *)(iobj + 1);
  451. struct nouveau_mem *node = *mem;
  452. return bar->umap(bar, node, flags, vma);
  453. }
  454. void
  455. nvimem_flush(struct drm_device *dev)
  456. {
  457. }
  458. void _nv50_vm_flush_engine(struct drm_device *dev, int engine)
  459. {
  460. struct nouveau_drm *drm = nouveau_newpriv(dev);
  461. nv50_vm_flush_engine(nv_subdev(drm->device), engine);
  462. }
  463. int _nouveau_vm_new(struct drm_device *dev, u64 offset, u64 length,
  464. u64 mm_offset, struct nouveau_vm **pvm)
  465. {
  466. struct nouveau_drm *drm = nouveau_newpriv(dev);
  467. return nouveau_vm_new(nv_device(drm->device), offset, length, mm_offset, pvm);
  468. }
  469. #include <core/subdev/vm/nv04.h>
  470. struct nouveau_vm *
  471. nv04vm_ref(struct drm_device *dev)
  472. {
  473. struct nouveau_drm *drm = nouveau_newpriv(dev);
  474. struct nouveau_vmmgr *vmm = nouveau_vmmgr(drm->device);
  475. struct nv04_vmmgr_priv *priv = (void *)vmm;
  476. return priv->vm;
  477. }
  478. struct nouveau_gpuobj *
  479. nv04vm_refdma(struct drm_device *dev)
  480. {
  481. struct nouveau_gpuobj *gpuobj = NULL;
  482. nouveau_gpuobj_ref(nv04vm_ref(dev)->pgt[0].obj[0], &gpuobj);
  483. return gpuobj;
  484. }
  485. void
  486. nvvm_engref(struct nouveau_vm *vm, int eng, int ref)
  487. {
  488. atomic_add(ref, &vm->engref[eng]);
  489. }
  490. int
  491. nvvm_spg_shift(struct nouveau_vm *vm)
  492. {
  493. return vm->vmm->spg_shift;
  494. }
  495. int
  496. nvvm_lpg_shift(struct nouveau_vm *vm)
  497. {
  498. return vm->vmm->lpg_shift;
  499. }
  500. u64 nvgpuobj_addr(struct nouveau_object *object)
  501. {
  502. return nv_gpuobj(object)->addr;
  503. }
  504. struct drm_device *
  505. nouveau_drv(void *ptr)
  506. {
  507. struct nouveau_drm *drm = ptr;
  508. return drm->dev;
  509. }
  510. struct nouveau_channel *
  511. nvdrm_channel(struct drm_device *dev)
  512. {
  513. struct nouveau_drm *drm = nouveau_newpriv(dev);
  514. return drm->channel;
  515. }
  516. struct mutex *
  517. nvchan_mutex(struct nouveau_channel *chan)
  518. {
  519. return &chan->cli->mutex;
  520. }