ast_main.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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)
  59. {
  60. struct ast_private *ast = dev->dev_private;
  61. if (dev->pdev->device == PCI_CHIP_AST1180) {
  62. ast->chip = AST1100;
  63. DRM_INFO("AST 1180 detected\n");
  64. } else {
  65. if (dev->pdev->revision >= 0x20) {
  66. ast->chip = AST2300;
  67. DRM_INFO("AST 2300 detected\n");
  68. } else if (dev->pdev->revision >= 0x10) {
  69. uint32_t data;
  70. ast_write32(ast, 0xf004, 0x1e6e0000);
  71. ast_write32(ast, 0xf000, 0x1);
  72. data = ast_read32(ast, 0x1207c);
  73. switch (data & 0x0300) {
  74. case 0x0200:
  75. ast->chip = AST1100;
  76. DRM_INFO("AST 1100 detected\n");
  77. break;
  78. case 0x0100:
  79. ast->chip = AST2200;
  80. DRM_INFO("AST 2200 detected\n");
  81. break;
  82. case 0x0000:
  83. ast->chip = AST2150;
  84. DRM_INFO("AST 2150 detected\n");
  85. break;
  86. default:
  87. ast->chip = AST2100;
  88. DRM_INFO("AST 2100 detected\n");
  89. break;
  90. }
  91. ast->vga2_clone = false;
  92. } else {
  93. ast->chip = 2000;
  94. DRM_INFO("AST 2000 detected\n");
  95. }
  96. }
  97. return 0;
  98. }
  99. static int ast_get_dram_info(struct drm_device *dev)
  100. {
  101. struct ast_private *ast = dev->dev_private;
  102. uint32_t data, data2;
  103. uint32_t denum, num, div, ref_pll;
  104. ast_write32(ast, 0xf004, 0x1e6e0000);
  105. ast_write32(ast, 0xf000, 0x1);
  106. ast_write32(ast, 0x10000, 0xfc600309);
  107. do {
  108. ;
  109. } while (ast_read32(ast, 0x10000) != 0x01);
  110. data = ast_read32(ast, 0x10004);
  111. if (data & 0x400)
  112. ast->dram_bus_width = 16;
  113. else
  114. ast->dram_bus_width = 32;
  115. if (ast->chip == AST2300) {
  116. switch (data & 0x03) {
  117. case 0:
  118. ast->dram_type = AST_DRAM_512Mx16;
  119. break;
  120. default:
  121. case 1:
  122. ast->dram_type = AST_DRAM_1Gx16;
  123. break;
  124. case 2:
  125. ast->dram_type = AST_DRAM_2Gx16;
  126. break;
  127. case 3:
  128. ast->dram_type = AST_DRAM_4Gx16;
  129. break;
  130. }
  131. } else {
  132. switch (data & 0x0c) {
  133. case 0:
  134. case 4:
  135. ast->dram_type = AST_DRAM_512Mx16;
  136. break;
  137. case 8:
  138. if (data & 0x40)
  139. ast->dram_type = AST_DRAM_1Gx16;
  140. else
  141. ast->dram_type = AST_DRAM_512Mx32;
  142. break;
  143. case 0xc:
  144. ast->dram_type = AST_DRAM_1Gx32;
  145. break;
  146. }
  147. }
  148. data = ast_read32(ast, 0x10120);
  149. data2 = ast_read32(ast, 0x10170);
  150. if (data2 & 0x2000)
  151. ref_pll = 14318;
  152. else
  153. ref_pll = 12000;
  154. denum = data & 0x1f;
  155. num = (data & 0x3fe0) >> 5;
  156. data = (data & 0xc000) >> 14;
  157. switch (data) {
  158. case 3:
  159. div = 0x4;
  160. break;
  161. case 2:
  162. case 1:
  163. div = 0x2;
  164. break;
  165. default:
  166. div = 0x1;
  167. break;
  168. }
  169. ast->mclk = ref_pll * (num + 2) / (denum + 2) * (div * 1000);
  170. return 0;
  171. }
  172. static void ast_user_framebuffer_destroy(struct drm_framebuffer *fb)
  173. {
  174. struct ast_framebuffer *ast_fb = to_ast_framebuffer(fb);
  175. if (ast_fb->obj)
  176. drm_gem_object_unreference_unlocked(ast_fb->obj);
  177. drm_framebuffer_cleanup(fb);
  178. kfree(fb);
  179. }
  180. static const struct drm_framebuffer_funcs ast_fb_funcs = {
  181. .destroy = ast_user_framebuffer_destroy,
  182. };
  183. int ast_framebuffer_init(struct drm_device *dev,
  184. struct ast_framebuffer *ast_fb,
  185. struct drm_mode_fb_cmd2 *mode_cmd,
  186. struct drm_gem_object *obj)
  187. {
  188. int ret;
  189. drm_helper_mode_fill_fb_struct(&ast_fb->base, mode_cmd);
  190. ast_fb->obj = obj;
  191. ret = drm_framebuffer_init(dev, &ast_fb->base, &ast_fb_funcs);
  192. if (ret) {
  193. DRM_ERROR("framebuffer init failed %d\n", ret);
  194. return ret;
  195. }
  196. return 0;
  197. }
  198. static struct drm_framebuffer *
  199. ast_user_framebuffer_create(struct drm_device *dev,
  200. struct drm_file *filp,
  201. struct drm_mode_fb_cmd2 *mode_cmd)
  202. {
  203. struct drm_gem_object *obj;
  204. struct ast_framebuffer *ast_fb;
  205. int ret;
  206. obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
  207. if (obj == NULL)
  208. return ERR_PTR(-ENOENT);
  209. ast_fb = kzalloc(sizeof(*ast_fb), GFP_KERNEL);
  210. if (!ast_fb) {
  211. drm_gem_object_unreference_unlocked(obj);
  212. return ERR_PTR(-ENOMEM);
  213. }
  214. ret = ast_framebuffer_init(dev, ast_fb, mode_cmd, obj);
  215. if (ret) {
  216. drm_gem_object_unreference_unlocked(obj);
  217. kfree(ast_fb);
  218. return ERR_PTR(ret);
  219. }
  220. return &ast_fb->base;
  221. }
  222. static const struct drm_mode_config_funcs ast_mode_funcs = {
  223. .fb_create = ast_user_framebuffer_create,
  224. };
  225. static u32 ast_get_vram_info(struct drm_device *dev)
  226. {
  227. struct ast_private *ast = dev->dev_private;
  228. u8 jreg;
  229. ast_open_key(ast);
  230. jreg = ast_get_index_reg_mask(ast, AST_IO_CRTC_PORT, 0xaa, 0xff);
  231. switch (jreg & 3) {
  232. case 0: return AST_VIDMEM_SIZE_8M;
  233. case 1: return AST_VIDMEM_SIZE_16M;
  234. case 2: return AST_VIDMEM_SIZE_32M;
  235. case 3: return AST_VIDMEM_SIZE_64M;
  236. }
  237. return AST_VIDMEM_DEFAULT_SIZE;
  238. }
  239. int ast_driver_load(struct drm_device *dev, unsigned long flags)
  240. {
  241. struct ast_private *ast;
  242. int ret = 0;
  243. ast = kzalloc(sizeof(struct ast_private), GFP_KERNEL);
  244. if (!ast)
  245. return -ENOMEM;
  246. dev->dev_private = ast;
  247. ast->dev = dev;
  248. ast->regs = pci_iomap(dev->pdev, 1, 0);
  249. if (!ast->regs) {
  250. ret = -EIO;
  251. goto out_free;
  252. }
  253. ast->ioregs = pci_iomap(dev->pdev, 2, 0);
  254. if (!ast->ioregs) {
  255. ret = -EIO;
  256. goto out_free;
  257. }
  258. ast_detect_chip(dev);
  259. if (ast->chip != AST1180) {
  260. ast_get_dram_info(dev);
  261. ast->vram_size = ast_get_vram_info(dev);
  262. DRM_INFO("dram %d %d %d %08x\n", ast->mclk, ast->dram_type, ast->dram_bus_width, ast->vram_size);
  263. }
  264. ret = ast_mm_init(ast);
  265. if (ret)
  266. goto out_free;
  267. drm_mode_config_init(dev);
  268. dev->mode_config.funcs = (void *)&ast_mode_funcs;
  269. dev->mode_config.min_width = 0;
  270. dev->mode_config.min_height = 0;
  271. dev->mode_config.preferred_depth = 24;
  272. dev->mode_config.prefer_shadow = 1;
  273. if (ast->chip == AST2100 ||
  274. ast->chip == AST2200 ||
  275. ast->chip == AST2300 ||
  276. ast->chip == AST1180) {
  277. dev->mode_config.max_width = 1920;
  278. dev->mode_config.max_height = 2048;
  279. } else {
  280. dev->mode_config.max_width = 1600;
  281. dev->mode_config.max_height = 1200;
  282. }
  283. ret = ast_mode_init(dev);
  284. if (ret)
  285. goto out_free;
  286. ret = ast_fbdev_init(dev);
  287. if (ret)
  288. goto out_free;
  289. return 0;
  290. out_free:
  291. kfree(ast);
  292. dev->dev_private = NULL;
  293. return ret;
  294. }
  295. int ast_driver_unload(struct drm_device *dev)
  296. {
  297. struct ast_private *ast = dev->dev_private;
  298. ast_mode_fini(dev);
  299. ast_fbdev_fini(dev);
  300. drm_mode_config_cleanup(dev);
  301. ast_mm_fini(ast);
  302. pci_iounmap(dev->pdev, ast->ioregs);
  303. pci_iounmap(dev->pdev, ast->regs);
  304. kfree(ast);
  305. return 0;
  306. }
  307. int ast_gem_create(struct drm_device *dev,
  308. u32 size, bool iskernel,
  309. struct drm_gem_object **obj)
  310. {
  311. struct ast_bo *astbo;
  312. int ret;
  313. *obj = NULL;
  314. size = roundup(size, PAGE_SIZE);
  315. if (size == 0)
  316. return -EINVAL;
  317. ret = ast_bo_create(dev, size, 0, 0, &astbo);
  318. if (ret) {
  319. if (ret != -ERESTARTSYS)
  320. DRM_ERROR("failed to allocate GEM object\n");
  321. return ret;
  322. }
  323. *obj = &astbo->gem;
  324. return 0;
  325. }
  326. int ast_dumb_create(struct drm_file *file,
  327. struct drm_device *dev,
  328. struct drm_mode_create_dumb *args)
  329. {
  330. int ret;
  331. struct drm_gem_object *gobj;
  332. u32 handle;
  333. args->pitch = args->width * ((args->bpp + 7) / 8);
  334. args->size = args->pitch * args->height;
  335. ret = ast_gem_create(dev, args->size, false,
  336. &gobj);
  337. if (ret)
  338. return ret;
  339. ret = drm_gem_handle_create(file, gobj, &handle);
  340. drm_gem_object_unreference_unlocked(gobj);
  341. if (ret)
  342. return ret;
  343. args->handle = handle;
  344. return 0;
  345. }
  346. static void ast_bo_unref(struct ast_bo **bo)
  347. {
  348. struct ttm_buffer_object *tbo;
  349. if ((*bo) == NULL)
  350. return;
  351. tbo = &((*bo)->bo);
  352. ttm_bo_unref(&tbo);
  353. *bo = NULL;
  354. }
  355. void ast_gem_free_object(struct drm_gem_object *obj)
  356. {
  357. struct ast_bo *ast_bo = gem_to_ast_bo(obj);
  358. ast_bo_unref(&ast_bo);
  359. }
  360. static inline u64 ast_bo_mmap_offset(struct ast_bo *bo)
  361. {
  362. return drm_vma_node_offset_addr(&bo->bo.vma_node);
  363. }
  364. int
  365. ast_dumb_mmap_offset(struct drm_file *file,
  366. struct drm_device *dev,
  367. uint32_t handle,
  368. uint64_t *offset)
  369. {
  370. struct drm_gem_object *obj;
  371. int ret;
  372. struct ast_bo *bo;
  373. mutex_lock(&dev->struct_mutex);
  374. obj = drm_gem_object_lookup(dev, file, handle);
  375. if (obj == NULL) {
  376. ret = -ENOENT;
  377. goto out_unlock;
  378. }
  379. bo = gem_to_ast_bo(obj);
  380. *offset = ast_bo_mmap_offset(bo);
  381. drm_gem_object_unreference(obj);
  382. ret = 0;
  383. out_unlock:
  384. mutex_unlock(&dev->struct_mutex);
  385. return ret;
  386. }