ast_main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. * Copyright 2012 Red Hat Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sub license, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  15. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  16. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  17. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  18. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. *
  20. * The above copyright notice and this permission notice (including the
  21. * next paragraph) shall be included in all copies or substantial portions
  22. * of the Software.
  23. *
  24. */
  25. /*
  26. * Authors: Dave Airlie <airlied@redhat.com>
  27. */
  28. #include <drm/drmP.h>
  29. #include "ast_drv.h"
  30. #include <drm/drm_fb_helper.h>
  31. #include <drm/drm_crtc_helper.h>
  32. void ast_set_index_reg_mask(struct ast_private *ast,
  33. uint32_t base, uint8_t index,
  34. uint8_t mask, uint8_t val)
  35. {
  36. u8 tmp;
  37. ast_io_write8(ast, base, index);
  38. tmp = (ast_io_read8(ast, base + 1) & mask) | val;
  39. ast_set_index_reg(ast, base, index, tmp);
  40. }
  41. uint8_t ast_get_index_reg(struct ast_private *ast,
  42. uint32_t base, uint8_t index)
  43. {
  44. uint8_t ret;
  45. ast_io_write8(ast, base, index);
  46. ret = ast_io_read8(ast, base + 1);
  47. return ret;
  48. }
  49. uint8_t ast_get_index_reg_mask(struct ast_private *ast,
  50. uint32_t base, uint8_t index, uint8_t mask)
  51. {
  52. uint8_t ret;
  53. ast_io_write8(ast, base, index);
  54. ret = ast_io_read8(ast, base + 1) & mask;
  55. return ret;
  56. }
  57. static void ast_detect_config_mode(struct drm_device *dev, u32 *scu_rev)
  58. {
  59. struct device_node *np = dev->pdev->dev.of_node;
  60. struct ast_private *ast = dev->dev_private;
  61. uint32_t data, jregd0, jregd1;
  62. /* Defaults */
  63. ast->config_mode = ast_use_defaults;
  64. *scu_rev = 0xffffffff;
  65. /* Check if we have device-tree properties */
  66. if (np && !of_property_read_u32(np, "aspeed,scu-revision-id",
  67. scu_rev)) {
  68. /* We do, disable P2A access */
  69. ast->config_mode = ast_use_dt;
  70. DRM_INFO("Using device-tree for configuration\n");
  71. return;
  72. }
  73. /* Not all families have a P2A bridge */
  74. if (dev->pdev->device != PCI_CHIP_AST2000)
  75. return;
  76. /*
  77. * The BMC will set SCU 0x40 D[12] to 1 if the P2 bridge
  78. * is disabled. We force using P2A if VGA only mode bit
  79. * is set D[7]
  80. */
  81. jregd0 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  82. jregd1 = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  83. if (!(jregd0 & 0x80) || !(jregd1 & 0x10)) {
  84. /* Double check it's actually working */
  85. data = ast_read32(ast, 0xf004);
  86. if (data != 0xFFFFFFFF) {
  87. /* P2A works, grab silicon revision */
  88. ast->config_mode = ast_use_p2a;
  89. DRM_INFO("Using P2A bridge for configuration\n");
  90. /* Read SCU7c (silicon revision register) */
  91. ast_write32(ast, 0xf004, 0x1e6e0000);
  92. ast_write32(ast, 0xf000, 0x1);
  93. *scu_rev = ast_read32(ast, 0x1207c);
  94. return;
  95. }
  96. }
  97. /* We have a P2A bridge but it's disabled */
  98. DRM_INFO("P2A bridge disabled, using default configuration\n");
  99. }
  100. static int ast_detect_chip(struct drm_device *dev, bool *need_post)
  101. {
  102. struct ast_private *ast = dev->dev_private;
  103. uint32_t jreg, scu_rev;
  104. /*
  105. * If VGA isn't enabled, we need to enable now or subsequent
  106. * access to the scratch registers will fail. We also inform
  107. * our caller that it needs to POST the chip
  108. * (Assumption: VGA not enabled -> need to POST)
  109. */
  110. if (!ast_is_vga_enabled(dev)) {
  111. ast_enable_vga(dev);
  112. DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
  113. *need_post = true;
  114. } else
  115. *need_post = false;
  116. /* Enable extended register access */
  117. ast_open_key(ast);
  118. ast_enable_mmio(dev);
  119. /* Find out whether P2A works or whether to use device-tree */
  120. ast_detect_config_mode(dev, &scu_rev);
  121. /* Identify chipset */
  122. if (dev->pdev->device == PCI_CHIP_AST1180) {
  123. ast->chip = AST1100;
  124. DRM_INFO("AST 1180 detected\n");
  125. } else {
  126. if (dev->pdev->revision >= 0x40) {
  127. ast->chip = AST2500;
  128. DRM_INFO("AST 2500 detected\n");
  129. } else if (dev->pdev->revision >= 0x30) {
  130. ast->chip = AST2400;
  131. DRM_INFO("AST 2400 detected\n");
  132. } else if (dev->pdev->revision >= 0x20) {
  133. ast->chip = AST2300;
  134. DRM_INFO("AST 2300 detected\n");
  135. } else if (dev->pdev->revision >= 0x10) {
  136. switch (scu_rev & 0x0300) {
  137. case 0x0200:
  138. ast->chip = AST1100;
  139. DRM_INFO("AST 1100 detected\n");
  140. break;
  141. case 0x0100:
  142. ast->chip = AST2200;
  143. DRM_INFO("AST 2200 detected\n");
  144. break;
  145. case 0x0000:
  146. ast->chip = AST2150;
  147. DRM_INFO("AST 2150 detected\n");
  148. break;
  149. default:
  150. ast->chip = AST2100;
  151. DRM_INFO("AST 2100 detected\n");
  152. break;
  153. }
  154. ast->vga2_clone = false;
  155. } else {
  156. ast->chip = AST2000;
  157. DRM_INFO("AST 2000 detected\n");
  158. }
  159. }
  160. /* Check if we support wide screen */
  161. switch (ast->chip) {
  162. case AST1180:
  163. ast->support_wide_screen = true;
  164. break;
  165. case AST2000:
  166. ast->support_wide_screen = false;
  167. break;
  168. default:
  169. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  170. if (!(jreg & 0x80))
  171. ast->support_wide_screen = true;
  172. else if (jreg & 0x01)
  173. ast->support_wide_screen = true;
  174. else {
  175. ast->support_wide_screen = false;
  176. if (ast->chip == AST2300 &&
  177. (scu_rev & 0x300) == 0x0) /* ast1300 */
  178. ast->support_wide_screen = true;
  179. if (ast->chip == AST2400 &&
  180. (scu_rev & 0x300) == 0x100) /* ast1400 */
  181. ast->support_wide_screen = true;
  182. if (ast->chip == AST2500 &&
  183. scu_rev == 0x100) /* ast2510 */
  184. ast->support_wide_screen = true;
  185. }
  186. break;
  187. }
  188. /* Check 3rd Tx option (digital output afaik) */
  189. ast->tx_chip_type = AST_TX_NONE;
  190. /*
  191. * VGACRA3 Enhanced Color Mode Register, check if DVO is already
  192. * enabled, in that case, assume we have a SIL164 TMDS transmitter
  193. *
  194. * Don't make that assumption if we the chip wasn't enabled and
  195. * is at power-on reset, otherwise we'll incorrectly "detect" a
  196. * SIL164 when there is none.
  197. */
  198. if (!*need_post) {
  199. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
  200. if (jreg & 0x80)
  201. ast->tx_chip_type = AST_TX_SIL164;
  202. }
  203. if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
  204. /*
  205. * On AST2300 and 2400, look the configuration set by the SoC in
  206. * the SOC scratch register #1 bits 11:8 (interestingly marked
  207. * as "reserved" in the spec)
  208. */
  209. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  210. switch (jreg) {
  211. case 0x04:
  212. ast->tx_chip_type = AST_TX_SIL164;
  213. break;
  214. case 0x08:
  215. ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
  216. if (ast->dp501_fw_addr) {
  217. /* backup firmware */
  218. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  219. kfree(ast->dp501_fw_addr);
  220. ast->dp501_fw_addr = NULL;
  221. }
  222. }
  223. /* fallthrough */
  224. case 0x0c:
  225. ast->tx_chip_type = AST_TX_DP501;
  226. }
  227. }
  228. /* Print stuff for diagnostic purposes */
  229. switch(ast->tx_chip_type) {
  230. case AST_TX_SIL164:
  231. DRM_INFO("Using Sil164 TMDS transmitter\n");
  232. break;
  233. case AST_TX_DP501:
  234. DRM_INFO("Using DP501 DisplayPort transmitter\n");
  235. break;
  236. default:
  237. DRM_INFO("Analog VGA only\n");
  238. }
  239. return 0;
  240. }
  241. static int ast_get_dram_info(struct drm_device *dev)
  242. {
  243. struct device_node *np = dev->pdev->dev.of_node;
  244. struct ast_private *ast = dev->dev_private;
  245. uint32_t mcr_cfg, mcr_scu_mpll, mcr_scu_strap;
  246. uint32_t denum, num, div, ref_pll, dsel;
  247. switch (ast->config_mode) {
  248. case ast_use_dt:
  249. /*
  250. * If some properties are missing, use reasonable
  251. * defaults for AST2400
  252. */
  253. if (of_property_read_u32(np, "aspeed,mcr-configuration",
  254. &mcr_cfg))
  255. mcr_cfg = 0x00000577;
  256. if (of_property_read_u32(np, "aspeed,mcr-scu-mpll",
  257. &mcr_scu_mpll))
  258. mcr_scu_mpll = 0x000050C0;
  259. if (of_property_read_u32(np, "aspeed,mcr-scu-strap",
  260. &mcr_scu_strap))
  261. mcr_scu_strap = 0;
  262. break;
  263. case ast_use_p2a:
  264. ast_write32(ast, 0xf004, 0x1e6e0000);
  265. ast_write32(ast, 0xf000, 0x1);
  266. mcr_cfg = ast_read32(ast, 0x10004);
  267. mcr_scu_mpll = ast_read32(ast, 0x10120);
  268. mcr_scu_strap = ast_read32(ast, 0x10170);
  269. break;
  270. case ast_use_defaults:
  271. default:
  272. ast->dram_bus_width = 16;
  273. ast->dram_type = AST_DRAM_1Gx16;
  274. if (ast->chip == AST2500)
  275. ast->mclk = 800;
  276. else
  277. ast->mclk = 396;
  278. return 0;
  279. }
  280. if (mcr_cfg & 0x40)
  281. ast->dram_bus_width = 16;
  282. else
  283. ast->dram_bus_width = 32;
  284. if (ast->chip == AST2500) {
  285. switch (mcr_cfg & 0x03) {
  286. case 0:
  287. ast->dram_type = AST_DRAM_1Gx16;
  288. break;
  289. default:
  290. case 1:
  291. ast->dram_type = AST_DRAM_2Gx16;
  292. break;
  293. case 2:
  294. ast->dram_type = AST_DRAM_4Gx16;
  295. break;
  296. case 3:
  297. ast->dram_type = AST_DRAM_8Gx16;
  298. break;
  299. }
  300. } else if (ast->chip == AST2300 || ast->chip == AST2400) {
  301. switch (mcr_cfg & 0x03) {
  302. case 0:
  303. ast->dram_type = AST_DRAM_512Mx16;
  304. break;
  305. default:
  306. case 1:
  307. ast->dram_type = AST_DRAM_1Gx16;
  308. break;
  309. case 2:
  310. ast->dram_type = AST_DRAM_2Gx16;
  311. break;
  312. case 3:
  313. ast->dram_type = AST_DRAM_4Gx16;
  314. break;
  315. }
  316. } else {
  317. switch (mcr_cfg & 0x0c) {
  318. case 0:
  319. case 4:
  320. ast->dram_type = AST_DRAM_512Mx16;
  321. break;
  322. case 8:
  323. if (mcr_cfg & 0x40)
  324. ast->dram_type = AST_DRAM_1Gx16;
  325. else
  326. ast->dram_type = AST_DRAM_512Mx32;
  327. break;
  328. case 0xc:
  329. ast->dram_type = AST_DRAM_1Gx32;
  330. break;
  331. }
  332. }
  333. if (mcr_scu_strap & 0x2000)
  334. ref_pll = 14318;
  335. else
  336. ref_pll = 12000;
  337. denum = mcr_scu_mpll & 0x1f;
  338. num = (mcr_scu_mpll & 0x3fe0) >> 5;
  339. dsel = (mcr_scu_mpll & 0xc000) >> 14;
  340. switch (dsel) {
  341. case 3:
  342. div = 0x4;
  343. break;
  344. case 2:
  345. case 1:
  346. div = 0x2;
  347. break;
  348. default:
  349. div = 0x1;
  350. break;
  351. }
  352. ast->mclk = ref_pll * (num + 2) / ((denum + 2) * (div * 1000));
  353. return 0;
  354. }
  355. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  356. {
  357. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  358. drm_gem_object_put_unlocked(ast_fb->obj);
  359. drm_framebuffer_cleanup(fb);
  360. kfree(ast_fb);
  361. }
  362. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  363. .destroy = ast_user_framebuffer_destroy,
  364. };
  365. int ast_framebuffer_init(struct drm_device *dev,
  366. struct ast_framebuffer *ast_fb,
  367. const struct drm_mode_fb_cmd2 *mode_cmd,
  368. struct drm_gem_object *obj)
  369. {
  370. int ret;
  371. drm_helper_mode_fill_fb_struct(dev, &ast_fb->base, mode_cmd);
  372. ast_fb->obj = obj;
  373. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  374. if (ret) {
  375. DRM_ERROR("framebuffer init failed %d\n", ret);
  376. return ret;
  377. }
  378. return 0;
  379. }
  380. static struct drm_framebuffer *
  381. ast_user_framebuffer_create(struct drm_device *dev,
  382. struct drm_file *filp,
  383. const struct drm_mode_fb_cmd2 *mode_cmd)
  384. {
  385. struct drm_gem_object *obj;
  386. struct ast_framebuffer *ast_fb;
  387. int ret;
  388. obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
  389. if (obj == NULL)
  390. return ERR_PTR(-ENOENT);
  391. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  392. if (!ast_fb) {
  393. drm_gem_object_put_unlocked(obj);
  394. return ERR_PTR(-ENOMEM);
  395. }
  396. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  397. if (ret) {
  398. drm_gem_object_put_unlocked(obj);
  399. kfree(ast_fb);
  400. return ERR_PTR(ret);
  401. }
  402. return &ast_fb->base;
  403. }
  404. static const struct drm_mode_config_funcs ast_mode_funcs = {
  405. .fb_create = ast_user_framebuffer_create,
  406. };
  407. static u32 ast_get_vram_info(struct drm_device *dev)
  408. {
  409. struct ast_private *ast = dev->dev_private;
  410. u8 jreg;
  411. u32 vram_size;
  412. ast_open_key(ast);
  413. vram_size = AST_VIDMEM_DEFAULT_SIZE;
  414. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  415. switch (jreg & 3) {
  416. case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
  417. case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
  418. case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
  419. case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
  420. }
  421. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
  422. switch (jreg & 0x03) {
  423. case 1:
  424. vram_size -= 0x100000;
  425. break;
  426. case 2:
  427. vram_size -= 0x200000;
  428. break;
  429. case 3:
  430. vram_size -= 0x400000;
  431. break;
  432. }
  433. return vram_size;
  434. }
  435. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  436. {
  437. struct ast_private *ast;
  438. bool need_post;
  439. int ret = 0;
  440. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  441. if (!ast)
  442. return -ENOMEM;
  443. dev->dev_private = ast;
  444. ast->dev = dev;
  445. ast->regs = pci_iomap(dev->pdev, 1, 0);
  446. if (!ast->regs) {
  447. ret = -EIO;
  448. goto out_free;
  449. }
  450. /*
  451. * If we don't have IO space at all, use MMIO now and
  452. * assume the chip has MMIO enabled by default (rev 0x20
  453. * and higher).
  454. */
  455. if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
  456. DRM_INFO("platform has no IO space, trying MMIO\n");
  457. ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
  458. }
  459. /* "map" IO regs if the above hasn't done so already */
  460. if (!ast->ioregs) {
  461. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  462. if (!ast->ioregs) {
  463. ret = -EIO;
  464. goto out_free;
  465. }
  466. }
  467. ast_detect_chip(dev, &need_post);
  468. if (need_post)
  469. ast_post_gpu(dev);
  470. if (ast->chip != AST1180) {
  471. ret = ast_get_dram_info(dev);
  472. if (ret)
  473. goto out_free;
  474. ast->vram_size = ast_get_vram_info(dev);
  475. DRM_INFO("dram MCLK=%u Mhz type=%d bus_width=%d size=%08x\n",
  476. ast->mclk, ast->dram_type,
  477. ast->dram_bus_width, ast->vram_size);
  478. }
  479. ret = ast_mm_init(ast);
  480. if (ret)
  481. goto out_free;
  482. drm_mode_config_init(dev);
  483. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  484. dev->mode_config.min_width = 0;
  485. dev->mode_config.min_height = 0;
  486. dev->mode_config.preferred_depth = 24;
  487. dev->mode_config.prefer_shadow = 1;
  488. dev->mode_config.fb_base = pci_resource_start(ast->dev->pdev, 0);
  489. if (ast->chip == AST2100 ||
  490. ast->chip == AST2200 ||
  491. ast->chip == AST2300 ||
  492. ast->chip == AST2400 ||
  493. ast->chip == AST2500 ||
  494. ast->chip == AST1180) {
  495. dev->mode_config.max_width = 1920;
  496. dev->mode_config.max_height = 2048;
  497. } else {
  498. dev->mode_config.max_width = 1600;
  499. dev->mode_config.max_height = 1200;
  500. }
  501. ret = ast_mode_init(dev);
  502. if (ret)
  503. goto out_free;
  504. ret = ast_fbdev_init(dev);
  505. if (ret)
  506. goto out_free;
  507. return 0;
  508. out_free:
  509. kfree(ast);
  510. dev->dev_private = NULL;
  511. return ret;
  512. }
  513. void ast_driver_unload(struct drm_device *dev)
  514. {
  515. struct ast_private *ast = dev->dev_private;
  516. /* enable standard VGA decode */
  517. ast_set_index_reg(ast, AST_IO_CRTC_PORT, 0xa1, 0x04);
  518. ast_release_firmware(dev);
  519. kfree(ast->dp501_fw_addr);
  520. ast_mode_fini(dev);
  521. ast_fbdev_fini(dev);
  522. drm_mode_config_cleanup(dev);
  523. ast_mm_fini(ast);
  524. if (ast->ioregs != ast->regs + AST_IO_MM_OFFSET)
  525. pci_iounmap(dev->pdev, ast->ioregs);
  526. pci_iounmap(dev->pdev, ast->regs);
  527. kfree(ast);
  528. }
  529. int ast_gem_create(struct drm_device *dev,
  530. u32 size, bool iskernel,
  531. struct drm_gem_object **obj)
  532. {
  533. struct ast_bo *astbo;
  534. int ret;
  535. *obj = NULL;
  536. size = roundup(size, PAGE_SIZE);
  537. if (size == 0)
  538. return -EINVAL;
  539. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  540. if (ret) {
  541. if (ret != -ERESTARTSYS)
  542. DRM_ERROR("failed to allocate GEM object\n");
  543. return ret;
  544. }
  545. *obj = &astbo->gem;
  546. return 0;
  547. }
  548. int ast_dumb_create(struct drm_file *file,
  549. struct drm_device *dev,
  550. struct drm_mode_create_dumb *args)
  551. {
  552. int ret;
  553. struct drm_gem_object *gobj;
  554. u32 handle;
  555. args->pitch = args->width * ((args->bpp + 7) / 8);
  556. args->size = args->pitch * args->height;
  557. ret = ast_gem_create(dev, args->size, false,
  558. &gobj);
  559. if (ret)
  560. return ret;
  561. ret = drm_gem_handle_create(file, gobj, &handle);
  562. drm_gem_object_put_unlocked(gobj);
  563. if (ret)
  564. return ret;
  565. args->handle = handle;
  566. return 0;
  567. }
  568. static void ast_bo_unref(struct ast_bo **bo)
  569. {
  570. struct ttm_buffer_object *tbo;
  571. if ((*bo) == NULL)
  572. return;
  573. tbo = &((*bo)->bo);
  574. ttm_bo_unref(&tbo);
  575. *bo = NULL;
  576. }
  577. void ast_gem_free_object(struct drm_gem_object *obj)
  578. {
  579. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  580. ast_bo_unref(&ast_bo);
  581. }
  582. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  583. {
  584. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  585. }
  586. int
  587. ast_dumb_mmap_offset(struct drm_file *file,
  588. struct drm_device *dev,
  589. uint32_t handle,
  590. uint64_t *offset)
  591. {
  592. struct drm_gem_object *obj;
  593. struct ast_bo *bo;
  594. obj = drm_gem_object_lookup(file, handle);
  595. if (obj == NULL)
  596. return -ENOENT;
  597. bo = gem_to_ast_bo(obj);
  598. *offset = ast_bo_mmap_offset(bo);
  599. drm_gem_object_put_unlocked(obj);
  600. return 0;
  601. }