ast_main.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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. #include "ast_dram_tables.h"
  33. void ast_set_index_reg_mask(struct ast_private *ast,
  34. uint32_t base, uint8_t index,
  35. uint8_t mask, uint8_t val)
  36. {
  37. u8 tmp;
  38. ast_io_write8(ast, base, index);
  39. tmp = (ast_io_read8(ast, base + 1) & mask) | val;
  40. ast_set_index_reg(ast, base, index, tmp);
  41. }
  42. uint8_t ast_get_index_reg(struct ast_private *ast,
  43. uint32_t base, uint8_t index)
  44. {
  45. uint8_t ret;
  46. ast_io_write8(ast, base, index);
  47. ret = ast_io_read8(ast, base + 1);
  48. return ret;
  49. }
  50. uint8_t ast_get_index_reg_mask(struct ast_private *ast,
  51. uint32_t base, uint8_t index, uint8_t mask)
  52. {
  53. uint8_t ret;
  54. ast_io_write8(ast, base, index);
  55. ret = ast_io_read8(ast, base + 1) & mask;
  56. return ret;
  57. }
  58. static int ast_detect_chip(struct drm_device *dev, bool *need_post)
  59. {
  60. struct ast_private *ast = dev->dev_private;
  61. uint32_t data, jreg;
  62. ast_open_key(ast);
  63. if (dev->pdev->device == PCI_CHIP_AST1180) {
  64. ast->chip = AST1100;
  65. DRM_INFO("AST 1180 detected\n");
  66. } else {
  67. if (dev->pdev->revision >= 0x30) {
  68. ast->chip = AST2400;
  69. DRM_INFO("AST 2400 detected\n");
  70. } else if (dev->pdev->revision >= 0x20) {
  71. ast->chip = AST2300;
  72. DRM_INFO("AST 2300 detected\n");
  73. } else if (dev->pdev->revision >= 0x10) {
  74. uint32_t data;
  75. ast_write32(ast, 0xf004, 0x1e6e0000);
  76. ast_write32(ast, 0xf000, 0x1);
  77. data = ast_read32(ast, 0x1207c);
  78. switch (data & 0x0300) {
  79. case 0x0200:
  80. ast->chip = AST1100;
  81. DRM_INFO("AST 1100 detected\n");
  82. break;
  83. case 0x0100:
  84. ast->chip = AST2200;
  85. DRM_INFO("AST 2200 detected\n");
  86. break;
  87. case 0x0000:
  88. ast->chip = AST2150;
  89. DRM_INFO("AST 2150 detected\n");
  90. break;
  91. default:
  92. ast->chip = AST2100;
  93. DRM_INFO("AST 2100 detected\n");
  94. break;
  95. }
  96. ast->vga2_clone = false;
  97. } else {
  98. ast->chip = AST2000;
  99. DRM_INFO("AST 2000 detected\n");
  100. }
  101. }
  102. /*
  103. * If VGA isn't enabled, we need to enable now or subsequent
  104. * access to the scratch registers will fail. We also inform
  105. * our caller that it needs to POST the chip
  106. * (Assumption: VGA not enabled -> need to POST)
  107. */
  108. if (!ast_is_vga_enabled(dev)) {
  109. ast_enable_vga(dev);
  110. ast_enable_mmio(dev);
  111. DRM_INFO("VGA not enabled on entry, requesting chip POST\n");
  112. *need_post = true;
  113. } else
  114. *need_post = false;
  115. /* Check P2A Access */
  116. ast->DisableP2A = true;
  117. data = ast_read32(ast, 0xf004);
  118. if (data != 0xFFFFFFFF)
  119. ast->DisableP2A = false;
  120. /* Check if we support wide screen */
  121. switch (ast->chip) {
  122. case AST1180:
  123. ast->support_wide_screen = true;
  124. break;
  125. case AST2000:
  126. ast->support_wide_screen = false;
  127. break;
  128. default:
  129. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd0, 0xff);
  130. if (!(jreg & 0x80))
  131. ast->support_wide_screen = true;
  132. else if (jreg & 0x01)
  133. ast->support_wide_screen = true;
  134. else {
  135. ast->support_wide_screen = false;
  136. if (ast->DisableP2A == false) {
  137. /* Read SCU7c (silicon revision register) */
  138. ast_write32(ast, 0xf004, 0x1e6e0000);
  139. ast_write32(ast, 0xf000, 0x1);
  140. data = ast_read32(ast, 0x1207c);
  141. data &= 0x300;
  142. if (ast->chip == AST2300 && data == 0x0) /* ast1300 */
  143. ast->support_wide_screen = true;
  144. if (ast->chip == AST2400 && data == 0x100) /* ast1400 */
  145. ast->support_wide_screen = true;
  146. }
  147. }
  148. break;
  149. }
  150. /* Check 3rd Tx option (digital output afaik) */
  151. ast->tx_chip_type = AST_TX_NONE;
  152. /*
  153. * VGACRA3 Enhanced Color Mode Register, check if DVO is already
  154. * enabled, in that case, assume we have a SIL164 TMDS transmitter
  155. *
  156. * Don't make that assumption if we the chip wasn't enabled and
  157. * is at power-on reset, otherwise we'll incorrectly "detect" a
  158. * SIL164 when there is none.
  159. */
  160. if (!*need_post) {
  161. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xa3, 0xff);
  162. if (jreg & 0x80)
  163. ast->tx_chip_type = AST_TX_SIL164;
  164. }
  165. if ((ast->chip == AST2300) || (ast->chip == AST2400)) {
  166. /*
  167. * On AST2300 and 2400, look the configuration set by the SoC in
  168. * the SOC scratch register #1 bits 11:8 (interestingly marked
  169. * as "reserved" in the spec)
  170. */
  171. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xd1, 0xff);
  172. switch (jreg) {
  173. case 0x04:
  174. ast->tx_chip_type = AST_TX_SIL164;
  175. break;
  176. case 0x08:
  177. ast->dp501_fw_addr = kzalloc(32*1024, GFP_KERNEL);
  178. if (ast->dp501_fw_addr) {
  179. /* backup firmware */
  180. if (ast_backup_fw(dev, ast->dp501_fw_addr, 32*1024)) {
  181. kfree(ast->dp501_fw_addr);
  182. ast->dp501_fw_addr = NULL;
  183. }
  184. }
  185. /* fallthrough */
  186. case 0x0c:
  187. ast->tx_chip_type = AST_TX_DP501;
  188. }
  189. }
  190. /* Print stuff for diagnostic purposes */
  191. switch(ast->tx_chip_type) {
  192. case AST_TX_SIL164:
  193. DRM_INFO("Using Sil164 TMDS transmitter\n");
  194. break;
  195. case AST_TX_DP501:
  196. DRM_INFO("Using DP501 DisplayPort transmitter\n");
  197. break;
  198. default:
  199. DRM_INFO("Analog VGA only\n");
  200. }
  201. return 0;
  202. }
  203. static int ast_get_dram_info(struct drm_device *dev)
  204. {
  205. struct ast_private *ast = dev->dev_private;
  206. uint32_t data, data2;
  207. uint32_t denum, num, div, ref_pll;
  208. if (ast->DisableP2A)
  209. {
  210. ast->dram_bus_width = 16;
  211. ast->dram_type = AST_DRAM_1Gx16;
  212. ast->mclk = 396;
  213. }
  214. else
  215. {
  216. ast_write32(ast, 0xf004, 0x1e6e0000);
  217. ast_write32(ast, 0xf000, 0x1);
  218. data = ast_read32(ast, 0x10004);
  219. if (data & 0x40)
  220. ast->dram_bus_width = 16;
  221. else
  222. ast->dram_bus_width = 32;
  223. if (ast->chip == AST2300 || ast->chip == AST2400) {
  224. switch (data & 0x03) {
  225. case 0:
  226. ast->dram_type = AST_DRAM_512Mx16;
  227. break;
  228. default:
  229. case 1:
  230. ast->dram_type = AST_DRAM_1Gx16;
  231. break;
  232. case 2:
  233. ast->dram_type = AST_DRAM_2Gx16;
  234. break;
  235. case 3:
  236. ast->dram_type = AST_DRAM_4Gx16;
  237. break;
  238. }
  239. } else {
  240. switch (data & 0x0c) {
  241. case 0:
  242. case 4:
  243. ast->dram_type = AST_DRAM_512Mx16;
  244. break;
  245. case 8:
  246. if (data & 0x40)
  247. ast->dram_type = AST_DRAM_1Gx16;
  248. else
  249. ast->dram_type = AST_DRAM_512Mx32;
  250. break;
  251. case 0xc:
  252. ast->dram_type = AST_DRAM_1Gx32;
  253. break;
  254. }
  255. }
  256. data = ast_read32(ast, 0x10120);
  257. data2 = ast_read32(ast, 0x10170);
  258. if (data2 & 0x2000)
  259. ref_pll = 14318;
  260. else
  261. ref_pll = 12000;
  262. denum = data & 0x1f;
  263. num = (data & 0x3fe0) >> 5;
  264. data = (data & 0xc000) >> 14;
  265. switch (data) {
  266. case 3:
  267. div = 0x4;
  268. break;
  269. case 2:
  270. case 1:
  271. div = 0x2;
  272. break;
  273. default:
  274. div = 0x1;
  275. break;
  276. }
  277. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  278. }
  279. return 0;
  280. }
  281. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  282. {
  283. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  284. drm_gem_object_unreference_unlocked(ast_fb->obj);
  285. drm_framebuffer_cleanup(fb);
  286. kfree(fb);
  287. }
  288. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  289. .destroy = ast_user_framebuffer_destroy,
  290. };
  291. int ast_framebuffer_init(struct drm_device *dev,
  292. struct ast_framebuffer *ast_fb,
  293. const struct drm_mode_fb_cmd2 *mode_cmd,
  294. struct drm_gem_object *obj)
  295. {
  296. int ret;
  297. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  298. ast_fb->obj = obj;
  299. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  300. if (ret) {
  301. DRM_ERROR("framebuffer init failed %d\n", ret);
  302. return ret;
  303. }
  304. return 0;
  305. }
  306. static struct drm_framebuffer *
  307. ast_user_framebuffer_create(struct drm_device *dev,
  308. struct drm_file *filp,
  309. const struct drm_mode_fb_cmd2 *mode_cmd)
  310. {
  311. struct drm_gem_object *obj;
  312. struct ast_framebuffer *ast_fb;
  313. int ret;
  314. obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
  315. if (obj == NULL)
  316. return ERR_PTR(-ENOENT);
  317. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  318. if (!ast_fb) {
  319. drm_gem_object_unreference_unlocked(obj);
  320. return ERR_PTR(-ENOMEM);
  321. }
  322. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  323. if (ret) {
  324. drm_gem_object_unreference_unlocked(obj);
  325. kfree(ast_fb);
  326. return ERR_PTR(ret);
  327. }
  328. return &ast_fb->base;
  329. }
  330. static const struct drm_mode_config_funcs ast_mode_funcs = {
  331. .fb_create = ast_user_framebuffer_create,
  332. };
  333. static u32 ast_get_vram_info(struct drm_device *dev)
  334. {
  335. struct ast_private *ast = dev->dev_private;
  336. u8 jreg;
  337. u32 vram_size;
  338. ast_open_key(ast);
  339. vram_size = AST_VIDMEM_DEFAULT_SIZE;
  340. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  341. switch (jreg & 3) {
  342. case 0: vram_size = AST_VIDMEM_SIZE_8M; break;
  343. case 1: vram_size = AST_VIDMEM_SIZE_16M; break;
  344. case 2: vram_size = AST_VIDMEM_SIZE_32M; break;
  345. case 3: vram_size = AST_VIDMEM_SIZE_64M; break;
  346. }
  347. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0x99, 0xff);
  348. switch (jreg & 0x03) {
  349. case 1:
  350. vram_size -= 0x100000;
  351. break;
  352. case 2:
  353. vram_size -= 0x200000;
  354. break;
  355. case 3:
  356. vram_size -= 0x400000;
  357. break;
  358. }
  359. return vram_size;
  360. }
  361. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  362. {
  363. struct ast_private *ast;
  364. bool need_post;
  365. int ret = 0;
  366. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  367. if (!ast)
  368. return -ENOMEM;
  369. dev->dev_private = ast;
  370. ast->dev = dev;
  371. ast->regs = pci_iomap(dev->pdev, 1, 0);
  372. if (!ast->regs) {
  373. ret = -EIO;
  374. goto out_free;
  375. }
  376. /*
  377. * If we don't have IO space at all, use MMIO now and
  378. * assume the chip has MMIO enabled by default (rev 0x20
  379. * and higher).
  380. */
  381. if (!(pci_resource_flags(dev->pdev, 2) & IORESOURCE_IO)) {
  382. DRM_INFO("platform has no IO space, trying MMIO\n");
  383. ast->ioregs = ast->regs + AST_IO_MM_OFFSET;
  384. }
  385. /* "map" IO regs if the above hasn't done so already */
  386. if (!ast->ioregs) {
  387. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  388. if (!ast->ioregs) {
  389. ret = -EIO;
  390. goto out_free;
  391. }
  392. }
  393. ast_detect_chip(dev, &need_post);
  394. if (ast->chip != AST1180) {
  395. ret = ast_get_dram_info(dev);
  396. if (ret)
  397. goto out_free;
  398. ast->vram_size = ast_get_vram_info(dev);
  399. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  400. }
  401. if (need_post)
  402. ast_post_gpu(dev);
  403. ret = ast_mm_init(ast);
  404. if (ret)
  405. goto out_free;
  406. drm_mode_config_init(dev);
  407. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  408. dev->mode_config.min_width = 0;
  409. dev->mode_config.min_height = 0;
  410. dev->mode_config.preferred_depth = 24;
  411. dev->mode_config.prefer_shadow = 1;
  412. dev->mode_config.fb_base = pci_resource_start(ast->dev->pdev, 0);
  413. if (ast->chip == AST2100 ||
  414. ast->chip == AST2200 ||
  415. ast->chip == AST2300 ||
  416. ast->chip == AST2400 ||
  417. ast->chip == AST1180) {
  418. dev->mode_config.max_width = 1920;
  419. dev->mode_config.max_height = 2048;
  420. } else {
  421. dev->mode_config.max_width = 1600;
  422. dev->mode_config.max_height = 1200;
  423. }
  424. ret = ast_mode_init(dev);
  425. if (ret)
  426. goto out_free;
  427. ret = ast_fbdev_init(dev);
  428. if (ret)
  429. goto out_free;
  430. return 0;
  431. out_free:
  432. kfree(ast);
  433. dev->dev_private = NULL;
  434. return ret;
  435. }
  436. int ast_driver_unload(struct drm_device *dev)
  437. {
  438. struct ast_private *ast = dev->dev_private;
  439. kfree(ast->dp501_fw_addr);
  440. ast_mode_fini(dev);
  441. ast_fbdev_fini(dev);
  442. drm_mode_config_cleanup(dev);
  443. ast_mm_fini(ast);
  444. pci_iounmap(dev->pdev, ast->ioregs);
  445. pci_iounmap(dev->pdev, ast->regs);
  446. kfree(ast);
  447. return 0;
  448. }
  449. int ast_gem_create(struct drm_device *dev,
  450. u32 size, bool iskernel,
  451. struct drm_gem_object **obj)
  452. {
  453. struct ast_bo *astbo;
  454. int ret;
  455. *obj = NULL;
  456. size = roundup(size, PAGE_SIZE);
  457. if (size == 0)
  458. return -EINVAL;
  459. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  460. if (ret) {
  461. if (ret != -ERESTARTSYS)
  462. DRM_ERROR("failed to allocate GEM object\n");
  463. return ret;
  464. }
  465. *obj = &astbo->gem;
  466. return 0;
  467. }
  468. int ast_dumb_create(struct drm_file *file,
  469. struct drm_device *dev,
  470. struct drm_mode_create_dumb *args)
  471. {
  472. int ret;
  473. struct drm_gem_object *gobj;
  474. u32 handle;
  475. args->pitch = args->width * ((args->bpp + 7) / 8);
  476. args->size = args->pitch * args->height;
  477. ret = ast_gem_create(dev, args->size, false,
  478. &gobj);
  479. if (ret)
  480. return ret;
  481. ret = drm_gem_handle_create(file, gobj, &handle);
  482. drm_gem_object_unreference_unlocked(gobj);
  483. if (ret)
  484. return ret;
  485. args->handle = handle;
  486. return 0;
  487. }
  488. static void ast_bo_unref(struct ast_bo **bo)
  489. {
  490. struct ttm_buffer_object *tbo;
  491. if ((*bo) == NULL)
  492. return;
  493. tbo = &((*bo)->bo);
  494. ttm_bo_unref(&tbo);
  495. *bo = NULL;
  496. }
  497. void ast_gem_free_object(struct drm_gem_object *obj)
  498. {
  499. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  500. ast_bo_unref(&ast_bo);
  501. }
  502. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  503. {
  504. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  505. }
  506. int
  507. ast_dumb_mmap_offset(struct drm_file *file,
  508. struct drm_device *dev,
  509. uint32_t handle,
  510. uint64_t *offset)
  511. {
  512. struct drm_gem_object *obj;
  513. struct ast_bo *bo;
  514. obj = drm_gem_object_lookup(file, handle);
  515. if (obj == NULL)
  516. return -ENOENT;
  517. bo = gem_to_ast_bo(obj);
  518. *offset = ast_bo_mmap_offset(bo);
  519. drm_gem_object_unreference_unlocked(obj);
  520. return 0;
  521. }