radeon_kms.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <drm/drmP.h>
  29. #include "radeon.h"
  30. #include <drm/radeon_drm.h>
  31. #include "radeon_asic.h"
  32. #include <linux/vga_switcheroo.h>
  33. #include <linux/slab.h>
  34. #include <linux/pm_runtime.h>
  35. #include "radeon_kfd.h"
  36. #if defined(CONFIG_VGA_SWITCHEROO)
  37. bool radeon_has_atpx(void);
  38. #else
  39. static inline bool radeon_has_atpx(void) { return false; }
  40. #endif
  41. /**
  42. * radeon_driver_unload_kms - Main unload function for KMS.
  43. *
  44. * @dev: drm dev pointer
  45. *
  46. * This is the main unload function for KMS (all asics).
  47. * It calls radeon_modeset_fini() to tear down the
  48. * displays, and radeon_device_fini() to tear down
  49. * the rest of the device (CP, writeback, etc.).
  50. * Returns 0 on success.
  51. */
  52. int radeon_driver_unload_kms(struct drm_device *dev)
  53. {
  54. struct radeon_device *rdev = dev->dev_private;
  55. if (rdev == NULL)
  56. return 0;
  57. if (rdev->rmmio == NULL)
  58. goto done_free;
  59. pm_runtime_get_sync(dev->dev);
  60. radeon_kfd_device_fini(rdev);
  61. radeon_acpi_fini(rdev);
  62. radeon_modeset_fini(rdev);
  63. radeon_device_fini(rdev);
  64. done_free:
  65. kfree(rdev);
  66. dev->dev_private = NULL;
  67. return 0;
  68. }
  69. /**
  70. * radeon_driver_load_kms - Main load function for KMS.
  71. *
  72. * @dev: drm dev pointer
  73. * @flags: device flags
  74. *
  75. * This is the main load function for KMS (all asics).
  76. * It calls radeon_device_init() to set up the non-display
  77. * parts of the chip (asic init, CP, writeback, etc.), and
  78. * radeon_modeset_init() to set up the display parts
  79. * (crtcs, encoders, hotplug detect, etc.).
  80. * Returns 0 on success, error on failure.
  81. */
  82. int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags)
  83. {
  84. struct radeon_device *rdev;
  85. int r, acpi_status;
  86. rdev = kzalloc(sizeof(struct radeon_device), GFP_KERNEL);
  87. if (rdev == NULL) {
  88. return -ENOMEM;
  89. }
  90. dev->dev_private = (void *)rdev;
  91. /* update BUS flag */
  92. if (drm_pci_device_is_agp(dev)) {
  93. flags |= RADEON_IS_AGP;
  94. } else if (pci_is_pcie(dev->pdev)) {
  95. flags |= RADEON_IS_PCIE;
  96. } else {
  97. flags |= RADEON_IS_PCI;
  98. }
  99. if ((radeon_runtime_pm != 0) &&
  100. radeon_has_atpx() &&
  101. ((flags & RADEON_IS_IGP) == 0))
  102. flags |= RADEON_IS_PX;
  103. /* radeon_device_init should report only fatal error
  104. * like memory allocation failure or iomapping failure,
  105. * or memory manager initialization failure, it must
  106. * properly initialize the GPU MC controller and permit
  107. * VRAM allocation
  108. */
  109. r = radeon_device_init(rdev, dev, dev->pdev, flags);
  110. if (r) {
  111. dev_err(&dev->pdev->dev, "Fatal error during GPU init\n");
  112. goto out;
  113. }
  114. /* Again modeset_init should fail only on fatal error
  115. * otherwise it should provide enough functionalities
  116. * for shadowfb to run
  117. */
  118. r = radeon_modeset_init(rdev);
  119. if (r)
  120. dev_err(&dev->pdev->dev, "Fatal error during modeset init\n");
  121. /* Call ACPI methods: require modeset init
  122. * but failure is not fatal
  123. */
  124. if (!r) {
  125. acpi_status = radeon_acpi_init(rdev);
  126. if (acpi_status)
  127. dev_dbg(&dev->pdev->dev,
  128. "Error during ACPI methods call\n");
  129. }
  130. radeon_kfd_device_probe(rdev);
  131. radeon_kfd_device_init(rdev);
  132. if (radeon_is_px(dev)) {
  133. pm_runtime_use_autosuspend(dev->dev);
  134. pm_runtime_set_autosuspend_delay(dev->dev, 5000);
  135. pm_runtime_set_active(dev->dev);
  136. pm_runtime_allow(dev->dev);
  137. pm_runtime_mark_last_busy(dev->dev);
  138. pm_runtime_put_autosuspend(dev->dev);
  139. }
  140. out:
  141. if (r)
  142. radeon_driver_unload_kms(dev);
  143. return r;
  144. }
  145. /**
  146. * radeon_set_filp_rights - Set filp right.
  147. *
  148. * @dev: drm dev pointer
  149. * @owner: drm file
  150. * @applier: drm file
  151. * @value: value
  152. *
  153. * Sets the filp rights for the device (all asics).
  154. */
  155. static void radeon_set_filp_rights(struct drm_device *dev,
  156. struct drm_file **owner,
  157. struct drm_file *applier,
  158. uint32_t *value)
  159. {
  160. struct radeon_device *rdev = dev->dev_private;
  161. mutex_lock(&rdev->gem.mutex);
  162. if (*value == 1) {
  163. /* wants rights */
  164. if (!*owner)
  165. *owner = applier;
  166. } else if (*value == 0) {
  167. /* revokes rights */
  168. if (*owner == applier)
  169. *owner = NULL;
  170. }
  171. *value = *owner == applier ? 1 : 0;
  172. mutex_unlock(&rdev->gem.mutex);
  173. }
  174. /*
  175. * Userspace get information ioctl
  176. */
  177. /**
  178. * radeon_info_ioctl - answer a device specific request.
  179. *
  180. * @rdev: radeon device pointer
  181. * @data: request object
  182. * @filp: drm filp
  183. *
  184. * This function is used to pass device specific parameters to the userspace
  185. * drivers. Examples include: pci device id, pipeline parms, tiling params,
  186. * etc. (all asics).
  187. * Returns 0 on success, -EINVAL on failure.
  188. */
  189. static int radeon_info_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
  190. {
  191. struct radeon_device *rdev = dev->dev_private;
  192. struct drm_radeon_info *info = data;
  193. struct radeon_mode_info *minfo = &rdev->mode_info;
  194. uint32_t *value, value_tmp, *value_ptr, value_size;
  195. uint64_t value64;
  196. struct drm_crtc *crtc;
  197. int i, found;
  198. value_ptr = (uint32_t *)((unsigned long)info->value);
  199. value = &value_tmp;
  200. value_size = sizeof(uint32_t);
  201. switch (info->request) {
  202. case RADEON_INFO_DEVICE_ID:
  203. *value = dev->pdev->device;
  204. break;
  205. case RADEON_INFO_NUM_GB_PIPES:
  206. *value = rdev->num_gb_pipes;
  207. break;
  208. case RADEON_INFO_NUM_Z_PIPES:
  209. *value = rdev->num_z_pipes;
  210. break;
  211. case RADEON_INFO_ACCEL_WORKING:
  212. /* xf86-video-ati 6.13.0 relies on this being false for evergreen */
  213. if ((rdev->family >= CHIP_CEDAR) && (rdev->family <= CHIP_HEMLOCK))
  214. *value = false;
  215. else
  216. *value = rdev->accel_working;
  217. break;
  218. case RADEON_INFO_CRTC_FROM_ID:
  219. if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
  220. DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
  221. return -EFAULT;
  222. }
  223. for (i = 0, found = 0; i < rdev->num_crtc; i++) {
  224. crtc = (struct drm_crtc *)minfo->crtcs[i];
  225. if (crtc && crtc->base.id == *value) {
  226. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  227. *value = radeon_crtc->crtc_id;
  228. found = 1;
  229. break;
  230. }
  231. }
  232. if (!found) {
  233. DRM_DEBUG_KMS("unknown crtc id %d\n", *value);
  234. return -EINVAL;
  235. }
  236. break;
  237. case RADEON_INFO_ACCEL_WORKING2:
  238. if (rdev->family == CHIP_HAWAII) {
  239. if (rdev->accel_working) {
  240. if (rdev->new_fw)
  241. *value = 3;
  242. else
  243. *value = 2;
  244. } else {
  245. *value = 0;
  246. }
  247. } else {
  248. *value = rdev->accel_working;
  249. }
  250. break;
  251. case RADEON_INFO_TILING_CONFIG:
  252. if (rdev->family >= CHIP_BONAIRE)
  253. *value = rdev->config.cik.tile_config;
  254. else if (rdev->family >= CHIP_TAHITI)
  255. *value = rdev->config.si.tile_config;
  256. else if (rdev->family >= CHIP_CAYMAN)
  257. *value = rdev->config.cayman.tile_config;
  258. else if (rdev->family >= CHIP_CEDAR)
  259. *value = rdev->config.evergreen.tile_config;
  260. else if (rdev->family >= CHIP_RV770)
  261. *value = rdev->config.rv770.tile_config;
  262. else if (rdev->family >= CHIP_R600)
  263. *value = rdev->config.r600.tile_config;
  264. else {
  265. DRM_DEBUG_KMS("tiling config is r6xx+ only!\n");
  266. return -EINVAL;
  267. }
  268. break;
  269. case RADEON_INFO_WANT_HYPERZ:
  270. /* The "value" here is both an input and output parameter.
  271. * If the input value is 1, filp requests hyper-z access.
  272. * If the input value is 0, filp revokes its hyper-z access.
  273. *
  274. * When returning, the value is 1 if filp owns hyper-z access,
  275. * 0 otherwise. */
  276. if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
  277. DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
  278. return -EFAULT;
  279. }
  280. if (*value >= 2) {
  281. DRM_DEBUG_KMS("WANT_HYPERZ: invalid value %d\n", *value);
  282. return -EINVAL;
  283. }
  284. radeon_set_filp_rights(dev, &rdev->hyperz_filp, filp, value);
  285. break;
  286. case RADEON_INFO_WANT_CMASK:
  287. /* The same logic as Hyper-Z. */
  288. if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
  289. DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
  290. return -EFAULT;
  291. }
  292. if (*value >= 2) {
  293. DRM_DEBUG_KMS("WANT_CMASK: invalid value %d\n", *value);
  294. return -EINVAL;
  295. }
  296. radeon_set_filp_rights(dev, &rdev->cmask_filp, filp, value);
  297. break;
  298. case RADEON_INFO_CLOCK_CRYSTAL_FREQ:
  299. /* return clock value in KHz */
  300. if (rdev->asic->get_xclk)
  301. *value = radeon_get_xclk(rdev) * 10;
  302. else
  303. *value = rdev->clock.spll.reference_freq * 10;
  304. break;
  305. case RADEON_INFO_NUM_BACKENDS:
  306. if (rdev->family >= CHIP_BONAIRE)
  307. *value = rdev->config.cik.max_backends_per_se *
  308. rdev->config.cik.max_shader_engines;
  309. else if (rdev->family >= CHIP_TAHITI)
  310. *value = rdev->config.si.max_backends_per_se *
  311. rdev->config.si.max_shader_engines;
  312. else if (rdev->family >= CHIP_CAYMAN)
  313. *value = rdev->config.cayman.max_backends_per_se *
  314. rdev->config.cayman.max_shader_engines;
  315. else if (rdev->family >= CHIP_CEDAR)
  316. *value = rdev->config.evergreen.max_backends;
  317. else if (rdev->family >= CHIP_RV770)
  318. *value = rdev->config.rv770.max_backends;
  319. else if (rdev->family >= CHIP_R600)
  320. *value = rdev->config.r600.max_backends;
  321. else {
  322. return -EINVAL;
  323. }
  324. break;
  325. case RADEON_INFO_NUM_TILE_PIPES:
  326. if (rdev->family >= CHIP_BONAIRE)
  327. *value = rdev->config.cik.max_tile_pipes;
  328. else if (rdev->family >= CHIP_TAHITI)
  329. *value = rdev->config.si.max_tile_pipes;
  330. else if (rdev->family >= CHIP_CAYMAN)
  331. *value = rdev->config.cayman.max_tile_pipes;
  332. else if (rdev->family >= CHIP_CEDAR)
  333. *value = rdev->config.evergreen.max_tile_pipes;
  334. else if (rdev->family >= CHIP_RV770)
  335. *value = rdev->config.rv770.max_tile_pipes;
  336. else if (rdev->family >= CHIP_R600)
  337. *value = rdev->config.r600.max_tile_pipes;
  338. else {
  339. return -EINVAL;
  340. }
  341. break;
  342. case RADEON_INFO_FUSION_GART_WORKING:
  343. *value = 1;
  344. break;
  345. case RADEON_INFO_BACKEND_MAP:
  346. if (rdev->family >= CHIP_BONAIRE)
  347. *value = rdev->config.cik.backend_map;
  348. else if (rdev->family >= CHIP_TAHITI)
  349. *value = rdev->config.si.backend_map;
  350. else if (rdev->family >= CHIP_CAYMAN)
  351. *value = rdev->config.cayman.backend_map;
  352. else if (rdev->family >= CHIP_CEDAR)
  353. *value = rdev->config.evergreen.backend_map;
  354. else if (rdev->family >= CHIP_RV770)
  355. *value = rdev->config.rv770.backend_map;
  356. else if (rdev->family >= CHIP_R600)
  357. *value = rdev->config.r600.backend_map;
  358. else {
  359. return -EINVAL;
  360. }
  361. break;
  362. case RADEON_INFO_VA_START:
  363. /* this is where we report if vm is supported or not */
  364. if (rdev->family < CHIP_CAYMAN)
  365. return -EINVAL;
  366. *value = RADEON_VA_RESERVED_SIZE;
  367. break;
  368. case RADEON_INFO_IB_VM_MAX_SIZE:
  369. /* this is where we report if vm is supported or not */
  370. if (rdev->family < CHIP_CAYMAN)
  371. return -EINVAL;
  372. *value = RADEON_IB_VM_MAX_SIZE;
  373. break;
  374. case RADEON_INFO_MAX_PIPES:
  375. if (rdev->family >= CHIP_BONAIRE)
  376. *value = rdev->config.cik.max_cu_per_sh;
  377. else if (rdev->family >= CHIP_TAHITI)
  378. *value = rdev->config.si.max_cu_per_sh;
  379. else if (rdev->family >= CHIP_CAYMAN)
  380. *value = rdev->config.cayman.max_pipes_per_simd;
  381. else if (rdev->family >= CHIP_CEDAR)
  382. *value = rdev->config.evergreen.max_pipes;
  383. else if (rdev->family >= CHIP_RV770)
  384. *value = rdev->config.rv770.max_pipes;
  385. else if (rdev->family >= CHIP_R600)
  386. *value = rdev->config.r600.max_pipes;
  387. else {
  388. return -EINVAL;
  389. }
  390. break;
  391. case RADEON_INFO_TIMESTAMP:
  392. if (rdev->family < CHIP_R600) {
  393. DRM_DEBUG_KMS("timestamp is r6xx+ only!\n");
  394. return -EINVAL;
  395. }
  396. value = (uint32_t*)&value64;
  397. value_size = sizeof(uint64_t);
  398. value64 = radeon_get_gpu_clock_counter(rdev);
  399. break;
  400. case RADEON_INFO_MAX_SE:
  401. if (rdev->family >= CHIP_BONAIRE)
  402. *value = rdev->config.cik.max_shader_engines;
  403. else if (rdev->family >= CHIP_TAHITI)
  404. *value = rdev->config.si.max_shader_engines;
  405. else if (rdev->family >= CHIP_CAYMAN)
  406. *value = rdev->config.cayman.max_shader_engines;
  407. else if (rdev->family >= CHIP_CEDAR)
  408. *value = rdev->config.evergreen.num_ses;
  409. else
  410. *value = 1;
  411. break;
  412. case RADEON_INFO_MAX_SH_PER_SE:
  413. if (rdev->family >= CHIP_BONAIRE)
  414. *value = rdev->config.cik.max_sh_per_se;
  415. else if (rdev->family >= CHIP_TAHITI)
  416. *value = rdev->config.si.max_sh_per_se;
  417. else
  418. return -EINVAL;
  419. break;
  420. case RADEON_INFO_FASTFB_WORKING:
  421. *value = rdev->fastfb_working;
  422. break;
  423. case RADEON_INFO_RING_WORKING:
  424. if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
  425. DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
  426. return -EFAULT;
  427. }
  428. switch (*value) {
  429. case RADEON_CS_RING_GFX:
  430. case RADEON_CS_RING_COMPUTE:
  431. *value = rdev->ring[RADEON_RING_TYPE_GFX_INDEX].ready;
  432. break;
  433. case RADEON_CS_RING_DMA:
  434. *value = rdev->ring[R600_RING_TYPE_DMA_INDEX].ready;
  435. *value |= rdev->ring[CAYMAN_RING_TYPE_DMA1_INDEX].ready;
  436. break;
  437. case RADEON_CS_RING_UVD:
  438. *value = rdev->ring[R600_RING_TYPE_UVD_INDEX].ready;
  439. break;
  440. case RADEON_CS_RING_VCE:
  441. *value = rdev->ring[TN_RING_TYPE_VCE1_INDEX].ready;
  442. break;
  443. default:
  444. return -EINVAL;
  445. }
  446. break;
  447. case RADEON_INFO_SI_TILE_MODE_ARRAY:
  448. if (rdev->family >= CHIP_BONAIRE) {
  449. value = rdev->config.cik.tile_mode_array;
  450. value_size = sizeof(uint32_t)*32;
  451. } else if (rdev->family >= CHIP_TAHITI) {
  452. value = rdev->config.si.tile_mode_array;
  453. value_size = sizeof(uint32_t)*32;
  454. } else {
  455. DRM_DEBUG_KMS("tile mode array is si+ only!\n");
  456. return -EINVAL;
  457. }
  458. break;
  459. case RADEON_INFO_CIK_MACROTILE_MODE_ARRAY:
  460. if (rdev->family >= CHIP_BONAIRE) {
  461. value = rdev->config.cik.macrotile_mode_array;
  462. value_size = sizeof(uint32_t)*16;
  463. } else {
  464. DRM_DEBUG_KMS("macrotile mode array is cik+ only!\n");
  465. return -EINVAL;
  466. }
  467. break;
  468. case RADEON_INFO_SI_CP_DMA_COMPUTE:
  469. *value = 1;
  470. break;
  471. case RADEON_INFO_SI_BACKEND_ENABLED_MASK:
  472. if (rdev->family >= CHIP_BONAIRE) {
  473. *value = rdev->config.cik.backend_enable_mask;
  474. } else if (rdev->family >= CHIP_TAHITI) {
  475. *value = rdev->config.si.backend_enable_mask;
  476. } else {
  477. DRM_DEBUG_KMS("BACKEND_ENABLED_MASK is si+ only!\n");
  478. }
  479. break;
  480. case RADEON_INFO_MAX_SCLK:
  481. if ((rdev->pm.pm_method == PM_METHOD_DPM) &&
  482. rdev->pm.dpm_enabled)
  483. *value = rdev->pm.dpm.dyn_state.max_clock_voltage_on_ac.sclk * 10;
  484. else
  485. *value = rdev->pm.default_sclk * 10;
  486. break;
  487. case RADEON_INFO_VCE_FW_VERSION:
  488. *value = rdev->vce.fw_version;
  489. break;
  490. case RADEON_INFO_VCE_FB_VERSION:
  491. *value = rdev->vce.fb_version;
  492. break;
  493. case RADEON_INFO_NUM_BYTES_MOVED:
  494. value = (uint32_t*)&value64;
  495. value_size = sizeof(uint64_t);
  496. value64 = atomic64_read(&rdev->num_bytes_moved);
  497. break;
  498. case RADEON_INFO_VRAM_USAGE:
  499. value = (uint32_t*)&value64;
  500. value_size = sizeof(uint64_t);
  501. value64 = atomic64_read(&rdev->vram_usage);
  502. break;
  503. case RADEON_INFO_GTT_USAGE:
  504. value = (uint32_t*)&value64;
  505. value_size = sizeof(uint64_t);
  506. value64 = atomic64_read(&rdev->gtt_usage);
  507. break;
  508. case RADEON_INFO_ACTIVE_CU_COUNT:
  509. if (rdev->family >= CHIP_BONAIRE)
  510. *value = rdev->config.cik.active_cus;
  511. else if (rdev->family >= CHIP_TAHITI)
  512. *value = rdev->config.si.active_cus;
  513. else if (rdev->family >= CHIP_CAYMAN)
  514. *value = rdev->config.cayman.active_simds;
  515. else if (rdev->family >= CHIP_CEDAR)
  516. *value = rdev->config.evergreen.active_simds;
  517. else if (rdev->family >= CHIP_RV770)
  518. *value = rdev->config.rv770.active_simds;
  519. else if (rdev->family >= CHIP_R600)
  520. *value = rdev->config.r600.active_simds;
  521. else
  522. *value = 1;
  523. break;
  524. case RADEON_INFO_CURRENT_GPU_TEMP:
  525. /* get temperature in millidegrees C */
  526. if (rdev->asic->pm.get_temperature)
  527. *value = radeon_get_temperature(rdev);
  528. else
  529. *value = 0;
  530. break;
  531. case RADEON_INFO_CURRENT_GPU_SCLK:
  532. /* get sclk in Mhz */
  533. if (rdev->pm.dpm_enabled)
  534. *value = radeon_dpm_get_current_sclk(rdev) / 100;
  535. else
  536. *value = rdev->pm.current_sclk / 100;
  537. break;
  538. case RADEON_INFO_CURRENT_GPU_MCLK:
  539. /* get mclk in Mhz */
  540. if (rdev->pm.dpm_enabled)
  541. *value = radeon_dpm_get_current_mclk(rdev) / 100;
  542. else
  543. *value = rdev->pm.current_mclk / 100;
  544. break;
  545. case RADEON_INFO_READ_REG:
  546. if (copy_from_user(value, value_ptr, sizeof(uint32_t))) {
  547. DRM_ERROR("copy_from_user %s:%u\n", __func__, __LINE__);
  548. return -EFAULT;
  549. }
  550. if (radeon_get_allowed_info_register(rdev, *value, value))
  551. return -EINVAL;
  552. break;
  553. case RADEON_INFO_VA_UNMAP_WORKING:
  554. *value = true;
  555. break;
  556. case RADEON_INFO_GPU_RESET_COUNTER:
  557. *value = atomic_read(&rdev->gpu_reset_counter);
  558. break;
  559. default:
  560. DRM_DEBUG_KMS("Invalid request %d\n", info->request);
  561. return -EINVAL;
  562. }
  563. if (copy_to_user(value_ptr, (char*)value, value_size)) {
  564. DRM_ERROR("copy_to_user %s:%u\n", __func__, __LINE__);
  565. return -EFAULT;
  566. }
  567. return 0;
  568. }
  569. /*
  570. * Outdated mess for old drm with Xorg being in charge (void function now).
  571. */
  572. /**
  573. * radeon_driver_lastclose_kms - drm callback for last close
  574. *
  575. * @dev: drm dev pointer
  576. *
  577. * Switch vga_switcheroo state after last close (all asics).
  578. */
  579. void radeon_driver_lastclose_kms(struct drm_device *dev)
  580. {
  581. struct radeon_device *rdev = dev->dev_private;
  582. radeon_fbdev_restore_mode(rdev);
  583. vga_switcheroo_process_delayed_switch();
  584. }
  585. /**
  586. * radeon_driver_open_kms - drm callback for open
  587. *
  588. * @dev: drm dev pointer
  589. * @file_priv: drm file
  590. *
  591. * On device open, init vm on cayman+ (all asics).
  592. * Returns 0 on success, error on failure.
  593. */
  594. int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv)
  595. {
  596. struct radeon_device *rdev = dev->dev_private;
  597. int r;
  598. file_priv->driver_priv = NULL;
  599. r = pm_runtime_get_sync(dev->dev);
  600. if (r < 0)
  601. return r;
  602. /* new gpu have virtual address space support */
  603. if (rdev->family >= CHIP_CAYMAN) {
  604. struct radeon_fpriv *fpriv;
  605. struct radeon_vm *vm;
  606. int r;
  607. fpriv = kzalloc(sizeof(*fpriv), GFP_KERNEL);
  608. if (unlikely(!fpriv)) {
  609. return -ENOMEM;
  610. }
  611. if (rdev->accel_working) {
  612. vm = &fpriv->vm;
  613. r = radeon_vm_init(rdev, vm);
  614. if (r) {
  615. kfree(fpriv);
  616. return r;
  617. }
  618. r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
  619. if (r) {
  620. radeon_vm_fini(rdev, vm);
  621. kfree(fpriv);
  622. return r;
  623. }
  624. /* map the ib pool buffer read only into
  625. * virtual address space */
  626. vm->ib_bo_va = radeon_vm_bo_add(rdev, vm,
  627. rdev->ring_tmp_bo.bo);
  628. r = radeon_vm_bo_set_addr(rdev, vm->ib_bo_va,
  629. RADEON_VA_IB_OFFSET,
  630. RADEON_VM_PAGE_READABLE |
  631. RADEON_VM_PAGE_SNOOPED);
  632. if (r) {
  633. radeon_vm_fini(rdev, vm);
  634. kfree(fpriv);
  635. return r;
  636. }
  637. }
  638. file_priv->driver_priv = fpriv;
  639. }
  640. pm_runtime_mark_last_busy(dev->dev);
  641. pm_runtime_put_autosuspend(dev->dev);
  642. return 0;
  643. }
  644. /**
  645. * radeon_driver_postclose_kms - drm callback for post close
  646. *
  647. * @dev: drm dev pointer
  648. * @file_priv: drm file
  649. *
  650. * On device post close, tear down vm on cayman+ (all asics).
  651. */
  652. void radeon_driver_postclose_kms(struct drm_device *dev,
  653. struct drm_file *file_priv)
  654. {
  655. struct radeon_device *rdev = dev->dev_private;
  656. /* new gpu have virtual address space support */
  657. if (rdev->family >= CHIP_CAYMAN && file_priv->driver_priv) {
  658. struct radeon_fpriv *fpriv = file_priv->driver_priv;
  659. struct radeon_vm *vm = &fpriv->vm;
  660. int r;
  661. if (rdev->accel_working) {
  662. r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
  663. if (!r) {
  664. if (vm->ib_bo_va)
  665. radeon_vm_bo_rmv(rdev, vm->ib_bo_va);
  666. radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
  667. }
  668. radeon_vm_fini(rdev, vm);
  669. }
  670. kfree(fpriv);
  671. file_priv->driver_priv = NULL;
  672. }
  673. }
  674. /**
  675. * radeon_driver_preclose_kms - drm callback for pre close
  676. *
  677. * @dev: drm dev pointer
  678. * @file_priv: drm file
  679. *
  680. * On device pre close, tear down hyperz and cmask filps on r1xx-r5xx
  681. * (all asics).
  682. */
  683. void radeon_driver_preclose_kms(struct drm_device *dev,
  684. struct drm_file *file_priv)
  685. {
  686. struct radeon_device *rdev = dev->dev_private;
  687. mutex_lock(&rdev->gem.mutex);
  688. if (rdev->hyperz_filp == file_priv)
  689. rdev->hyperz_filp = NULL;
  690. if (rdev->cmask_filp == file_priv)
  691. rdev->cmask_filp = NULL;
  692. mutex_unlock(&rdev->gem.mutex);
  693. radeon_uvd_free_handles(rdev, file_priv);
  694. radeon_vce_free_handles(rdev, file_priv);
  695. }
  696. /*
  697. * VBlank related functions.
  698. */
  699. /**
  700. * radeon_get_vblank_counter_kms - get frame count
  701. *
  702. * @dev: drm dev pointer
  703. * @crtc: crtc to get the frame count from
  704. *
  705. * Gets the frame count on the requested crtc (all asics).
  706. * Returns frame count on success, -EINVAL on failure.
  707. */
  708. u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc)
  709. {
  710. int vpos, hpos, stat;
  711. u32 count;
  712. struct radeon_device *rdev = dev->dev_private;
  713. if (crtc < 0 || crtc >= rdev->num_crtc) {
  714. DRM_ERROR("Invalid crtc %d\n", crtc);
  715. return -EINVAL;
  716. }
  717. /* The hw increments its frame counter at start of vsync, not at start
  718. * of vblank, as is required by DRM core vblank counter handling.
  719. * Cook the hw count here to make it appear to the caller as if it
  720. * incremented at start of vblank. We measure distance to start of
  721. * vblank in vpos. vpos therefore will be >= 0 between start of vblank
  722. * and start of vsync, so vpos >= 0 means to bump the hw frame counter
  723. * result by 1 to give the proper appearance to caller.
  724. */
  725. if (rdev->mode_info.crtcs[crtc]) {
  726. /* Repeat readout if needed to provide stable result if
  727. * we cross start of vsync during the queries.
  728. */
  729. do {
  730. count = radeon_get_vblank_counter(rdev, crtc);
  731. /* Ask radeon_get_crtc_scanoutpos to return vpos as
  732. * distance to start of vblank, instead of regular
  733. * vertical scanout pos.
  734. */
  735. stat = radeon_get_crtc_scanoutpos(
  736. dev, crtc, GET_DISTANCE_TO_VBLANKSTART,
  737. &vpos, &hpos, NULL, NULL,
  738. &rdev->mode_info.crtcs[crtc]->base.hwmode);
  739. } while (count != radeon_get_vblank_counter(rdev, crtc));
  740. if (((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  741. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE))) {
  742. DRM_DEBUG_VBL("Query failed! stat %d\n", stat);
  743. }
  744. else {
  745. DRM_DEBUG_VBL("crtc %d: dist from vblank start %d\n",
  746. crtc, vpos);
  747. /* Bump counter if we are at >= leading edge of vblank,
  748. * but before vsync where vpos would turn negative and
  749. * the hw counter really increments.
  750. */
  751. if (vpos >= 0)
  752. count++;
  753. }
  754. }
  755. else {
  756. /* Fallback to use value as is. */
  757. count = radeon_get_vblank_counter(rdev, crtc);
  758. DRM_DEBUG_VBL("NULL mode info! Returned count may be wrong.\n");
  759. }
  760. return count;
  761. }
  762. /**
  763. * radeon_enable_vblank_kms - enable vblank interrupt
  764. *
  765. * @dev: drm dev pointer
  766. * @crtc: crtc to enable vblank interrupt for
  767. *
  768. * Enable the interrupt on the requested crtc (all asics).
  769. * Returns 0 on success, -EINVAL on failure.
  770. */
  771. int radeon_enable_vblank_kms(struct drm_device *dev, int crtc)
  772. {
  773. struct radeon_device *rdev = dev->dev_private;
  774. unsigned long irqflags;
  775. int r;
  776. if (crtc < 0 || crtc >= rdev->num_crtc) {
  777. DRM_ERROR("Invalid crtc %d\n", crtc);
  778. return -EINVAL;
  779. }
  780. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  781. rdev->irq.crtc_vblank_int[crtc] = true;
  782. r = radeon_irq_set(rdev);
  783. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  784. return r;
  785. }
  786. /**
  787. * radeon_disable_vblank_kms - disable vblank interrupt
  788. *
  789. * @dev: drm dev pointer
  790. * @crtc: crtc to disable vblank interrupt for
  791. *
  792. * Disable the interrupt on the requested crtc (all asics).
  793. */
  794. void radeon_disable_vblank_kms(struct drm_device *dev, int crtc)
  795. {
  796. struct radeon_device *rdev = dev->dev_private;
  797. unsigned long irqflags;
  798. if (crtc < 0 || crtc >= rdev->num_crtc) {
  799. DRM_ERROR("Invalid crtc %d\n", crtc);
  800. return;
  801. }
  802. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  803. rdev->irq.crtc_vblank_int[crtc] = false;
  804. radeon_irq_set(rdev);
  805. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  806. }
  807. /**
  808. * radeon_get_vblank_timestamp_kms - get vblank timestamp
  809. *
  810. * @dev: drm dev pointer
  811. * @crtc: crtc to get the timestamp for
  812. * @max_error: max error
  813. * @vblank_time: time value
  814. * @flags: flags passed to the driver
  815. *
  816. * Gets the timestamp on the requested crtc based on the
  817. * scanout position. (all asics).
  818. * Returns postive status flags on success, negative error on failure.
  819. */
  820. int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
  821. int *max_error,
  822. struct timeval *vblank_time,
  823. unsigned flags)
  824. {
  825. struct drm_crtc *drmcrtc;
  826. struct radeon_device *rdev = dev->dev_private;
  827. if (crtc < 0 || crtc >= dev->num_crtcs) {
  828. DRM_ERROR("Invalid crtc %d\n", crtc);
  829. return -EINVAL;
  830. }
  831. /* Get associated drm_crtc: */
  832. drmcrtc = &rdev->mode_info.crtcs[crtc]->base;
  833. if (!drmcrtc)
  834. return -EINVAL;
  835. /* Helper routine in DRM core does all the work: */
  836. return drm_calc_vbltimestamp_from_scanoutpos(dev, crtc, max_error,
  837. vblank_time, flags,
  838. &drmcrtc->hwmode);
  839. }
  840. const struct drm_ioctl_desc radeon_ioctls_kms[] = {
  841. DRM_IOCTL_DEF_DRV(RADEON_CP_INIT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  842. DRM_IOCTL_DEF_DRV(RADEON_CP_START, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  843. DRM_IOCTL_DEF_DRV(RADEON_CP_STOP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  844. DRM_IOCTL_DEF_DRV(RADEON_CP_RESET, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  845. DRM_IOCTL_DEF_DRV(RADEON_CP_IDLE, drm_invalid_op, DRM_AUTH),
  846. DRM_IOCTL_DEF_DRV(RADEON_CP_RESUME, drm_invalid_op, DRM_AUTH),
  847. DRM_IOCTL_DEF_DRV(RADEON_RESET, drm_invalid_op, DRM_AUTH),
  848. DRM_IOCTL_DEF_DRV(RADEON_FULLSCREEN, drm_invalid_op, DRM_AUTH),
  849. DRM_IOCTL_DEF_DRV(RADEON_SWAP, drm_invalid_op, DRM_AUTH),
  850. DRM_IOCTL_DEF_DRV(RADEON_CLEAR, drm_invalid_op, DRM_AUTH),
  851. DRM_IOCTL_DEF_DRV(RADEON_VERTEX, drm_invalid_op, DRM_AUTH),
  852. DRM_IOCTL_DEF_DRV(RADEON_INDICES, drm_invalid_op, DRM_AUTH),
  853. DRM_IOCTL_DEF_DRV(RADEON_TEXTURE, drm_invalid_op, DRM_AUTH),
  854. DRM_IOCTL_DEF_DRV(RADEON_STIPPLE, drm_invalid_op, DRM_AUTH),
  855. DRM_IOCTL_DEF_DRV(RADEON_INDIRECT, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  856. DRM_IOCTL_DEF_DRV(RADEON_VERTEX2, drm_invalid_op, DRM_AUTH),
  857. DRM_IOCTL_DEF_DRV(RADEON_CMDBUF, drm_invalid_op, DRM_AUTH),
  858. DRM_IOCTL_DEF_DRV(RADEON_GETPARAM, drm_invalid_op, DRM_AUTH),
  859. DRM_IOCTL_DEF_DRV(RADEON_FLIP, drm_invalid_op, DRM_AUTH),
  860. DRM_IOCTL_DEF_DRV(RADEON_ALLOC, drm_invalid_op, DRM_AUTH),
  861. DRM_IOCTL_DEF_DRV(RADEON_FREE, drm_invalid_op, DRM_AUTH),
  862. DRM_IOCTL_DEF_DRV(RADEON_INIT_HEAP, drm_invalid_op, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
  863. DRM_IOCTL_DEF_DRV(RADEON_IRQ_EMIT, drm_invalid_op, DRM_AUTH),
  864. DRM_IOCTL_DEF_DRV(RADEON_IRQ_WAIT, drm_invalid_op, DRM_AUTH),
  865. DRM_IOCTL_DEF_DRV(RADEON_SETPARAM, drm_invalid_op, DRM_AUTH),
  866. DRM_IOCTL_DEF_DRV(RADEON_SURF_ALLOC, drm_invalid_op, DRM_AUTH),
  867. DRM_IOCTL_DEF_DRV(RADEON_SURF_FREE, drm_invalid_op, DRM_AUTH),
  868. /* KMS */
  869. DRM_IOCTL_DEF_DRV(RADEON_GEM_INFO, radeon_gem_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  870. DRM_IOCTL_DEF_DRV(RADEON_GEM_CREATE, radeon_gem_create_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  871. DRM_IOCTL_DEF_DRV(RADEON_GEM_MMAP, radeon_gem_mmap_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  872. DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_DOMAIN, radeon_gem_set_domain_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  873. DRM_IOCTL_DEF_DRV(RADEON_GEM_PREAD, radeon_gem_pread_ioctl, DRM_AUTH),
  874. DRM_IOCTL_DEF_DRV(RADEON_GEM_PWRITE, radeon_gem_pwrite_ioctl, DRM_AUTH),
  875. DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_IDLE, radeon_gem_wait_idle_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  876. DRM_IOCTL_DEF_DRV(RADEON_CS, radeon_cs_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  877. DRM_IOCTL_DEF_DRV(RADEON_INFO, radeon_info_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  878. DRM_IOCTL_DEF_DRV(RADEON_GEM_SET_TILING, radeon_gem_set_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  879. DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  880. DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  881. DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  882. DRM_IOCTL_DEF_DRV(RADEON_GEM_OP, radeon_gem_op_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  883. DRM_IOCTL_DEF_DRV(RADEON_GEM_USERPTR, radeon_gem_userptr_ioctl, DRM_AUTH|DRM_RENDER_ALLOW),
  884. };
  885. int radeon_max_kms_ioctl = ARRAY_SIZE(radeon_ioctls_kms);