ast_main.c 13 KB

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