pvr_device.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
  2. /* Copyright (c) 2023 Imagination Technologies Ltd. */
  3. #ifndef PVR_DEVICE_H
  4. #define PVR_DEVICE_H
  5. #include "pvr_ccb.h"
  6. #include "pvr_device_info.h"
  7. #include "pvr_fw.h"
  8. #include "pvr_rogue_fwif_stream.h"
  9. #include "pvr_stream.h"
  10. #include <drm/drm_device.h>
  11. #include <drm/drm_file.h>
  12. #include <drm/drm_mm.h>
  13. #include <linux/bits.h>
  14. #include <linux/compiler_attributes.h>
  15. #include <linux/compiler_types.h>
  16. #include <linux/device.h>
  17. #include <linux/io.h>
  18. #include <linux/iopoll.h>
  19. #include <linux/kernel.h>
  20. #include <linux/math.h>
  21. #include <linux/mutex.h>
  22. #include <linux/spinlock_types.h>
  23. #include <linux/timer.h>
  24. #include <linux/types.h>
  25. #include <linux/wait.h>
  26. #include <linux/workqueue.h>
  27. #include <linux/xarray.h>
  28. /* Forward declaration from <linux/clk.h>. */
  29. struct clk;
  30. /* Forward declaration from <linux/firmware.h>. */
  31. struct firmware;
  32. /* Forward declaration from <linux/pwrseq/consumer.h> */
  33. struct pwrseq_desc;
  34. #define PVR_GPUID_STRING_MIN_LENGTH 7U
  35. #define PVR_GPUID_STRING_MAX_LENGTH 32U
  36. /**
  37. * struct pvr_gpu_id - Hardware GPU ID information for a PowerVR device
  38. * @b: Branch ID.
  39. * @v: Version ID.
  40. * @n: Number of scalable units.
  41. * @c: Config ID.
  42. */
  43. struct pvr_gpu_id {
  44. u16 b, v, n, c;
  45. };
  46. /**
  47. * struct pvr_fw_version - Firmware version information
  48. * @major: Major version number.
  49. * @minor: Minor version number.
  50. */
  51. struct pvr_fw_version {
  52. u16 major, minor;
  53. };
  54. /**
  55. * struct pvr_device_data - Platform specific data associated with a compatible string.
  56. * @pwr_ops: Pointer to a structure with platform-specific power functions.
  57. */
  58. struct pvr_device_data {
  59. const struct pvr_power_sequence_ops *pwr_ops;
  60. };
  61. /**
  62. * struct pvr_device - powervr-specific wrapper for &struct drm_device
  63. */
  64. struct pvr_device {
  65. /**
  66. * @base: The underlying &struct drm_device.
  67. *
  68. * Do not access this member directly, instead call
  69. * from_pvr_device().
  70. */
  71. struct drm_device base;
  72. /** @gpu_id: GPU ID detected at runtime. */
  73. struct pvr_gpu_id gpu_id;
  74. /**
  75. * @features: Hardware feature information.
  76. *
  77. * Do not access this member directly, instead use PVR_HAS_FEATURE()
  78. * or PVR_FEATURE_VALUE() macros.
  79. */
  80. struct pvr_device_features features;
  81. /**
  82. * @quirks: Hardware quirk information.
  83. *
  84. * Do not access this member directly, instead use PVR_HAS_QUIRK().
  85. */
  86. struct pvr_device_quirks quirks;
  87. /**
  88. * @enhancements: Hardware enhancement information.
  89. *
  90. * Do not access this member directly, instead use
  91. * PVR_HAS_ENHANCEMENT().
  92. */
  93. struct pvr_device_enhancements enhancements;
  94. /** @fw_version: Firmware version detected at runtime. */
  95. struct pvr_fw_version fw_version;
  96. /** @device_data: Pointer to platform-specific data. */
  97. const struct pvr_device_data *device_data;
  98. /** @regs_resource: Resource representing device control registers. */
  99. struct resource *regs_resource;
  100. /**
  101. * @regs: Device control registers.
  102. *
  103. * These are mapped into memory when the device is initialized; that
  104. * location is where this pointer points.
  105. */
  106. void __iomem *regs;
  107. /**
  108. * @core_clk: General core clock.
  109. *
  110. * This is the primary clock used by the entire GPU core.
  111. */
  112. struct clk *core_clk;
  113. /**
  114. * @sys_clk: Optional system bus clock.
  115. *
  116. * This may be used on some platforms to provide an independent clock to the SoC Interface
  117. * (SOCIF). If present, this needs to be enabled/disabled together with @core_clk.
  118. */
  119. struct clk *sys_clk;
  120. /**
  121. * @mem_clk: Optional memory clock.
  122. *
  123. * This may be used on some platforms to provide an independent clock to the Memory
  124. * Interface (MEMIF). If present, this needs to be enabled/disabled together with @core_clk.
  125. */
  126. struct clk *mem_clk;
  127. /**
  128. * @power: Optional power domain devices.
  129. *
  130. * On platforms with more than one power domain for the GPU, they are
  131. * stored here in @domain_devs, along with links between them in
  132. * @domain_links. The size of @domain_devs is given by @domain_count,
  133. * while the size of @domain_links is (2 * @domain_count) - 1.
  134. */
  135. struct pvr_device_power {
  136. struct device **domain_devs;
  137. struct device_link **domain_links;
  138. u32 domain_count;
  139. } power;
  140. /**
  141. * @reset: Optional reset line.
  142. *
  143. * This may be used on some platforms to provide a reset line that needs to be de-asserted
  144. * after power-up procedure. It would also need to be asserted after the power-down
  145. * procedure.
  146. */
  147. struct reset_control *reset;
  148. /** @pwrseq: Pointer to a power sequencer, if one is used. */
  149. struct pwrseq_desc *pwrseq;
  150. /** @irq: IRQ number. */
  151. int irq;
  152. /** @fwccb: Firmware CCB. */
  153. struct pvr_ccb fwccb;
  154. /**
  155. * @kernel_vm_ctx: Virtual memory context used for kernel mappings.
  156. *
  157. * This is used for mappings in the firmware address region when a META firmware processor
  158. * is in use.
  159. *
  160. * When a MIPS firmware processor is in use, this will be %NULL.
  161. */
  162. struct pvr_vm_context *kernel_vm_ctx;
  163. /** @fw_dev: Firmware related data. */
  164. struct pvr_fw_device fw_dev;
  165. /** @stream_musthave_quirks: Bit array of "must-have" quirks for stream commands. */
  166. u32 stream_musthave_quirks[PVR_STREAM_TYPE_MAX][PVR_STREAM_EXTHDR_TYPE_MAX];
  167. /**
  168. * @mmu_flush_cache_flags: Records which MMU caches require flushing
  169. * before submitting the next job.
  170. */
  171. atomic_t mmu_flush_cache_flags;
  172. /**
  173. * @ctx_ids: Array of contexts belonging to this device. Array members
  174. * are of type "struct pvr_context *".
  175. *
  176. * This array is used to allocate IDs used by the firmware.
  177. */
  178. struct xarray ctx_ids;
  179. /**
  180. * @free_list_ids: Array of free lists belonging to this device. Array members
  181. * are of type "struct pvr_free_list *".
  182. *
  183. * This array is used to allocate IDs used by the firmware.
  184. */
  185. struct xarray free_list_ids;
  186. /**
  187. * @job_ids: Array of jobs belonging to this device. Array members
  188. * are of type "struct pvr_job *".
  189. */
  190. struct xarray job_ids;
  191. /**
  192. * @queues: Queue-related fields.
  193. */
  194. struct {
  195. /** @queues.active: Active queue list. */
  196. struct list_head active;
  197. /** @queues.idle: Idle queue list. */
  198. struct list_head idle;
  199. /** @queues.lock: Lock protecting access to the active/idle
  200. * lists. */
  201. struct mutex lock;
  202. } queues;
  203. /**
  204. * @watchdog: Watchdog for communications with firmware.
  205. */
  206. struct {
  207. /** @watchdog.work: Work item for watchdog callback. */
  208. struct delayed_work work;
  209. /**
  210. * @watchdog.old_kccb_cmds_executed: KCCB command execution
  211. * count at last watchdog poll.
  212. */
  213. u32 old_kccb_cmds_executed;
  214. /**
  215. * @watchdog.kccb_stall_count: Number of watchdog polls
  216. * KCCB has been stalled for.
  217. */
  218. u32 kccb_stall_count;
  219. } watchdog;
  220. /**
  221. * @kccb: Circular buffer for communications with firmware.
  222. */
  223. struct {
  224. /** @kccb.ccb: Kernel CCB. */
  225. struct pvr_ccb ccb;
  226. /** @kccb.rtn_q: Waitqueue for KCCB command return waiters. */
  227. wait_queue_head_t rtn_q;
  228. /** @kccb.rtn_obj: Object representing KCCB return slots. */
  229. struct pvr_fw_object *rtn_obj;
  230. /**
  231. * @kccb.rtn: Pointer to CPU mapping of KCCB return slots.
  232. * Must be accessed by READ_ONCE()/WRITE_ONCE().
  233. */
  234. u32 *rtn;
  235. /** @kccb.slot_count: Total number of KCCB slots available. */
  236. u32 slot_count;
  237. /** @kccb.reserved_count: Number of KCCB slots reserved for
  238. * future use. */
  239. u32 reserved_count;
  240. /**
  241. * @kccb.waiters: List of KCCB slot waiters.
  242. */
  243. struct list_head waiters;
  244. /** @kccb.fence_ctx: KCCB fence context. */
  245. struct {
  246. /** @kccb.fence_ctx.id: KCCB fence context ID
  247. * allocated with dma_fence_context_alloc(). */
  248. u64 id;
  249. /** @kccb.fence_ctx.seqno: Sequence number incremented
  250. * each time a fence is created. */
  251. atomic_t seqno;
  252. /**
  253. * @kccb.fence_ctx.lock: Lock used to synchronize
  254. * access to fences allocated by this context.
  255. */
  256. spinlock_t lock;
  257. } fence_ctx;
  258. } kccb;
  259. /**
  260. * @lost: %true if the device has been lost.
  261. *
  262. * This variable is set if the device has become irretrievably unavailable, e.g. if the
  263. * firmware processor has stopped responding and can not be revived via a hard reset.
  264. */
  265. bool lost;
  266. /**
  267. * @reset_sem: Reset semaphore.
  268. *
  269. * GPU reset code will lock this for writing. Any code that submits commands to the firmware
  270. * that isn't in an IRQ handler or on the scheduler workqueue must lock this for reading.
  271. * Once this has been successfully locked, &pvr_dev->lost _must_ be checked, and -%EIO must
  272. * be returned if it is set.
  273. */
  274. struct rw_semaphore reset_sem;
  275. /** @sched_wq: Workqueue for schedulers. */
  276. struct workqueue_struct *sched_wq;
  277. /**
  278. * @ctx_list_lock: Lock to be held when accessing the context list in
  279. * struct pvr_file.
  280. */
  281. spinlock_t ctx_list_lock;
  282. /** @has_safety_events: Whether this device can raise safety events. */
  283. bool has_safety_events;
  284. };
  285. /**
  286. * struct pvr_file - powervr-specific data to be assigned to &struct
  287. * drm_file.driver_priv
  288. */
  289. struct pvr_file {
  290. /**
  291. * @file: A reference to the parent &struct drm_file.
  292. *
  293. * Do not access this member directly, instead call from_pvr_file().
  294. */
  295. struct drm_file *file;
  296. /**
  297. * @pvr_dev: A reference to the powervr-specific wrapper for the
  298. * associated device. Saves on repeated calls to to_pvr_device().
  299. */
  300. struct pvr_device *pvr_dev;
  301. /**
  302. * @ctx_handles: Array of contexts belonging to this file. Array members
  303. * are of type "struct pvr_context *".
  304. *
  305. * This array is used to allocate handles returned to userspace.
  306. */
  307. struct xarray ctx_handles;
  308. /**
  309. * @free_list_handles: Array of free lists belonging to this file. Array
  310. * members are of type "struct pvr_free_list *".
  311. *
  312. * This array is used to allocate handles returned to userspace.
  313. */
  314. struct xarray free_list_handles;
  315. /**
  316. * @hwrt_handles: Array of HWRT datasets belonging to this file. Array
  317. * members are of type "struct pvr_hwrt_dataset *".
  318. *
  319. * This array is used to allocate handles returned to userspace.
  320. */
  321. struct xarray hwrt_handles;
  322. /**
  323. * @vm_ctx_handles: Array of VM contexts belonging to this file. Array
  324. * members are of type "struct pvr_vm_context *".
  325. *
  326. * This array is used to allocate handles returned to userspace.
  327. */
  328. struct xarray vm_ctx_handles;
  329. /** @contexts: PVR context list. */
  330. struct list_head contexts;
  331. };
  332. /**
  333. * PVR_HAS_FEATURE() - Tests whether a PowerVR device has a given feature
  334. * @pvr_dev: [IN] Target PowerVR device.
  335. * @feature: [IN] Hardware feature name.
  336. *
  337. * Feature names are derived from those found in &struct pvr_device_features by
  338. * dropping the 'has_' prefix, which is applied by this macro.
  339. *
  340. * Return:
  341. * * true if the named feature is present in the hardware
  342. * * false if the named feature is not present in the hardware
  343. */
  344. #define PVR_HAS_FEATURE(pvr_dev, feature) ((pvr_dev)->features.has_##feature)
  345. /**
  346. * PVR_FEATURE_VALUE() - Gets a PowerVR device feature value
  347. * @pvr_dev: [IN] Target PowerVR device.
  348. * @feature: [IN] Feature name.
  349. * @value_out: [OUT] Feature value.
  350. *
  351. * This macro will get a feature value for those features that have values.
  352. * If the feature is not present, nothing will be stored to @value_out.
  353. *
  354. * Feature names are derived from those found in &struct pvr_device_features by
  355. * dropping the 'has_' prefix.
  356. *
  357. * Return:
  358. * * 0 on success, or
  359. * * -%EINVAL if the named feature is not present in the hardware
  360. */
  361. #define PVR_FEATURE_VALUE(pvr_dev, feature, value_out) \
  362. ({ \
  363. struct pvr_device *_pvr_dev = pvr_dev; \
  364. int _ret = -EINVAL; \
  365. if (_pvr_dev->features.has_##feature) { \
  366. *(value_out) = _pvr_dev->features.feature; \
  367. _ret = 0; \
  368. } \
  369. _ret; \
  370. })
  371. /**
  372. * PVR_HAS_QUIRK() - Tests whether a physical device has a given quirk
  373. * @pvr_dev: [IN] Target PowerVR device.
  374. * @quirk: [IN] Hardware quirk name.
  375. *
  376. * Quirk numbers are derived from those found in #pvr_device_quirks by
  377. * dropping the 'has_brn' prefix, which is applied by this macro.
  378. *
  379. * Returns
  380. * * true if the quirk is present in the hardware, or
  381. * * false if the quirk is not present in the hardware.
  382. */
  383. #define PVR_HAS_QUIRK(pvr_dev, quirk) ((pvr_dev)->quirks.has_brn##quirk)
  384. /**
  385. * PVR_HAS_ENHANCEMENT() - Tests whether a physical device has a given
  386. * enhancement
  387. * @pvr_dev: [IN] Target PowerVR device.
  388. * @enhancement: [IN] Hardware enhancement name.
  389. *
  390. * Enhancement numbers are derived from those found in #pvr_device_enhancements
  391. * by dropping the 'has_ern' prefix, which is applied by this macro.
  392. *
  393. * Returns
  394. * * true if the enhancement is present in the hardware, or
  395. * * false if the enhancement is not present in the hardware.
  396. */
  397. #define PVR_HAS_ENHANCEMENT(pvr_dev, enhancement) ((pvr_dev)->enhancements.has_ern##enhancement)
  398. #define from_pvr_device(pvr_dev) (&(pvr_dev)->base)
  399. #define to_pvr_device(drm_dev) container_of_const(drm_dev, struct pvr_device, base)
  400. #define from_pvr_file(pvr_file) ((pvr_file)->file)
  401. #define to_pvr_file(file) ((file)->driver_priv)
  402. /**
  403. * PVR_PACKED_BVNC() - Packs B, V, N and C values into a 64-bit unsigned integer
  404. * @b: Branch ID.
  405. * @v: Version ID.
  406. * @n: Number of scalable units.
  407. * @c: Config ID.
  408. *
  409. * The packed layout is as follows:
  410. *
  411. * +--------+--------+--------+-------+
  412. * | 63..48 | 47..32 | 31..16 | 15..0 |
  413. * +========+========+========+=======+
  414. * | B | V | N | C |
  415. * +--------+--------+--------+-------+
  416. *
  417. * pvr_gpu_id_to_packed_bvnc() should be used instead of this macro when a
  418. * &struct pvr_gpu_id is available in order to ensure proper type checking.
  419. *
  420. * Return: Packed BVNC.
  421. */
  422. /* clang-format off */
  423. #define PVR_PACKED_BVNC(b, v, n, c) \
  424. ((((u64)(b) & GENMASK_ULL(15, 0)) << 48) | \
  425. (((u64)(v) & GENMASK_ULL(15, 0)) << 32) | \
  426. (((u64)(n) & GENMASK_ULL(15, 0)) << 16) | \
  427. (((u64)(c) & GENMASK_ULL(15, 0)) << 0))
  428. /* clang-format on */
  429. /**
  430. * pvr_gpu_id_to_packed_bvnc() - Packs B, V, N and C values into a 64-bit
  431. * unsigned integer
  432. * @gpu_id: GPU ID.
  433. *
  434. * The packed layout is as follows:
  435. *
  436. * +--------+--------+--------+-------+
  437. * | 63..48 | 47..32 | 31..16 | 15..0 |
  438. * +========+========+========+=======+
  439. * | B | V | N | C |
  440. * +--------+--------+--------+-------+
  441. *
  442. * This should be used in preference to PVR_PACKED_BVNC() when a &struct
  443. * pvr_gpu_id is available in order to ensure proper type checking.
  444. *
  445. * Return: Packed BVNC.
  446. */
  447. static __always_inline u64
  448. pvr_gpu_id_to_packed_bvnc(const struct pvr_gpu_id *gpu_id)
  449. {
  450. return PVR_PACKED_BVNC(gpu_id->b, gpu_id->v, gpu_id->n, gpu_id->c);
  451. }
  452. static __always_inline void
  453. packed_bvnc_to_pvr_gpu_id(u64 bvnc, struct pvr_gpu_id *gpu_id)
  454. {
  455. gpu_id->b = (bvnc & GENMASK_ULL(63, 48)) >> 48;
  456. gpu_id->v = (bvnc & GENMASK_ULL(47, 32)) >> 32;
  457. gpu_id->n = (bvnc & GENMASK_ULL(31, 16)) >> 16;
  458. gpu_id->c = bvnc & GENMASK_ULL(15, 0);
  459. }
  460. int pvr_device_init(struct pvr_device *pvr_dev);
  461. void pvr_device_fini(struct pvr_device *pvr_dev);
  462. void pvr_device_reset(struct pvr_device *pvr_dev);
  463. bool
  464. pvr_device_has_uapi_quirk(struct pvr_device *pvr_dev, u32 quirk);
  465. bool
  466. pvr_device_has_uapi_enhancement(struct pvr_device *pvr_dev, u32 enhancement);
  467. bool
  468. pvr_device_has_feature(struct pvr_device *pvr_dev, u32 feature);
  469. #if IS_ENABLED(CONFIG_KUNIT)
  470. int pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
  471. const char *param_bvnc, struct pvr_gpu_id *gpu_id);
  472. #endif
  473. /**
  474. * PVR_CR_FIELD_GET() - Extract a single field from a PowerVR control register
  475. * @val: Value of the target register.
  476. * @field: Field specifier, as defined in "pvr_rogue_cr_defs.h".
  477. *
  478. * Return: The extracted field.
  479. */
  480. #define PVR_CR_FIELD_GET(val, field) FIELD_GET(~ROGUE_CR_##field##_CLRMSK, val)
  481. /**
  482. * pvr_cr_read32() - Read a 32-bit register from a PowerVR device
  483. * @pvr_dev: Target PowerVR device.
  484. * @reg: Target register.
  485. *
  486. * Return: The value of the requested register.
  487. */
  488. static __always_inline u32
  489. pvr_cr_read32(const struct pvr_device *pvr_dev, u32 reg)
  490. {
  491. return ioread32(pvr_dev->regs + reg);
  492. }
  493. /**
  494. * pvr_cr_read64() - Read a 64-bit register from a PowerVR device
  495. * @pvr_dev: Target PowerVR device.
  496. * @reg: Target register.
  497. *
  498. * Return: The value of the requested register.
  499. */
  500. static __always_inline u64
  501. pvr_cr_read64(const struct pvr_device *pvr_dev, u32 reg)
  502. {
  503. return ioread64(pvr_dev->regs + reg);
  504. }
  505. /**
  506. * pvr_cr_write32() - Write to a 32-bit register in a PowerVR device
  507. * @pvr_dev: Target PowerVR device.
  508. * @reg: Target register.
  509. * @val: Value to write.
  510. */
  511. static __always_inline void
  512. pvr_cr_write32(struct pvr_device *pvr_dev, u32 reg, u32 val)
  513. {
  514. iowrite32(val, pvr_dev->regs + reg);
  515. }
  516. /**
  517. * pvr_cr_write64() - Write to a 64-bit register in a PowerVR device
  518. * @pvr_dev: Target PowerVR device.
  519. * @reg: Target register.
  520. * @val: Value to write.
  521. */
  522. static __always_inline void
  523. pvr_cr_write64(struct pvr_device *pvr_dev, u32 reg, u64 val)
  524. {
  525. iowrite64(val, pvr_dev->regs + reg);
  526. }
  527. /**
  528. * pvr_cr_poll_reg32() - Wait for a 32-bit register to match a given value by
  529. * polling
  530. * @pvr_dev: Target PowerVR device.
  531. * @reg_addr: Address of register.
  532. * @reg_value: Expected register value (after masking).
  533. * @reg_mask: Mask of bits valid for comparison with @reg_value.
  534. * @timeout_usec: Timeout length, in us.
  535. *
  536. * Returns:
  537. * * 0 on success, or
  538. * * -%ETIMEDOUT on timeout.
  539. */
  540. static __always_inline int
  541. pvr_cr_poll_reg32(struct pvr_device *pvr_dev, u32 reg_addr, u32 reg_value,
  542. u32 reg_mask, u64 timeout_usec)
  543. {
  544. u32 value;
  545. return readl_poll_timeout(pvr_dev->regs + reg_addr, value,
  546. (value & reg_mask) == reg_value, 0, timeout_usec);
  547. }
  548. /**
  549. * pvr_cr_poll_reg64() - Wait for a 64-bit register to match a given value by
  550. * polling
  551. * @pvr_dev: Target PowerVR device.
  552. * @reg_addr: Address of register.
  553. * @reg_value: Expected register value (after masking).
  554. * @reg_mask: Mask of bits valid for comparison with @reg_value.
  555. * @timeout_usec: Timeout length, in us.
  556. *
  557. * Returns:
  558. * * 0 on success, or
  559. * * -%ETIMEDOUT on timeout.
  560. */
  561. static __always_inline int
  562. pvr_cr_poll_reg64(struct pvr_device *pvr_dev, u32 reg_addr, u64 reg_value,
  563. u64 reg_mask, u64 timeout_usec)
  564. {
  565. u64 value;
  566. return readq_poll_timeout(pvr_dev->regs + reg_addr, value,
  567. (value & reg_mask) == reg_value, 0, timeout_usec);
  568. }
  569. /**
  570. * pvr_round_up_to_cacheline_size() - Round up a provided size to be cacheline
  571. * aligned
  572. * @pvr_dev: Target PowerVR device.
  573. * @size: Initial size, in bytes.
  574. *
  575. * Returns:
  576. * * Size aligned to cacheline size.
  577. */
  578. static __always_inline size_t
  579. pvr_round_up_to_cacheline_size(struct pvr_device *pvr_dev, size_t size)
  580. {
  581. u16 slc_cacheline_size_bits = 0;
  582. u16 slc_cacheline_size_bytes;
  583. WARN_ON(!PVR_HAS_FEATURE(pvr_dev, slc_cache_line_size_bits));
  584. PVR_FEATURE_VALUE(pvr_dev, slc_cache_line_size_bits,
  585. &slc_cacheline_size_bits);
  586. slc_cacheline_size_bytes = slc_cacheline_size_bits / 8;
  587. return round_up(size, slc_cacheline_size_bytes);
  588. }
  589. /**
  590. * DOC: IOCTL validation helpers
  591. *
  592. * To validate the constraints imposed on IOCTL argument structs, a collection
  593. * of macros and helper functions exist in ``pvr_device.h``.
  594. *
  595. * Of the current helpers, it should only be necessary to call
  596. * PVR_IOCTL_UNION_PADDING_CHECK() directly. This macro should be used once in
  597. * every code path which extracts a union member from a struct passed from
  598. * userspace.
  599. */
  600. /**
  601. * pvr_ioctl_union_padding_check() - Validate that the implicit padding between
  602. * the end of a union member and the end of the union itself is zeroed.
  603. * @instance: Pointer to the instance of the struct to validate.
  604. * @union_offset: Offset into the type of @instance of the target union. Must
  605. * be 64-bit aligned.
  606. * @union_size: Size of the target union in the type of @instance. Must be
  607. * 64-bit aligned.
  608. * @member_size: Size of the target member in the target union specified by
  609. * @union_offset and @union_size. It is assumed that the offset of the target
  610. * member is zero relative to @union_offset. Must be 64-bit aligned.
  611. *
  612. * You probably want to use PVR_IOCTL_UNION_PADDING_CHECK() instead of calling
  613. * this function directly, since that macro abstracts away much of the setup,
  614. * and also provides some static validation. See its docs for details.
  615. *
  616. * Return:
  617. * * %true if every byte between the end of the used member of the union and
  618. * the end of that union is zeroed, or
  619. * * %false otherwise.
  620. */
  621. static __always_inline bool
  622. pvr_ioctl_union_padding_check(void *instance, size_t union_offset,
  623. size_t union_size, size_t member_size)
  624. {
  625. /*
  626. * void pointer arithmetic is technically illegal - cast to a byte
  627. * pointer so this addition works safely.
  628. */
  629. void *padding_start = ((u8 *)instance) + union_offset + member_size;
  630. size_t padding_size = union_size - member_size;
  631. return mem_is_zero(padding_start, padding_size);
  632. }
  633. /**
  634. * PVR_STATIC_ASSERT_64BIT_ALIGNED() - Inline assertion for 64-bit alignment.
  635. * @static_expr_: Target expression to evaluate.
  636. *
  637. * If @static_expr_ does not evaluate to a constant integer which would be a
  638. * 64-bit aligned address (i.e. a multiple of 8), compilation will fail.
  639. *
  640. * Return:
  641. * The value of @static_expr_.
  642. */
  643. #define PVR_STATIC_ASSERT_64BIT_ALIGNED(static_expr_) \
  644. ({ \
  645. static_assert(((static_expr_) & (sizeof(u64) - 1)) == 0); \
  646. (static_expr_); \
  647. })
  648. /**
  649. * PVR_IOCTL_UNION_PADDING_CHECK() - Validate that the implicit padding between
  650. * the end of a union member and the end of the union itself is zeroed.
  651. * @struct_instance_: An expression which evaluates to a pointer to a UAPI data
  652. * struct.
  653. * @union_: The name of the union member of @struct_instance_ to check. If the
  654. * union member is nested within the type of @struct_instance_, this may
  655. * contain the member access operator (".").
  656. * @member_: The name of the member of @union_ to assess.
  657. *
  658. * This is a wrapper around pvr_ioctl_union_padding_check() which performs
  659. * alignment checks and simplifies things for the caller.
  660. *
  661. * Return:
  662. * * %true if every byte in @struct_instance_ between the end of @member_ and
  663. * the end of @union_ is zeroed, or
  664. * * %false otherwise.
  665. */
  666. #define PVR_IOCTL_UNION_PADDING_CHECK(struct_instance_, union_, member_) \
  667. ({ \
  668. typeof(struct_instance_) __instance = (struct_instance_); \
  669. size_t __union_offset = PVR_STATIC_ASSERT_64BIT_ALIGNED( \
  670. offsetof(typeof(*__instance), union_)); \
  671. size_t __union_size = PVR_STATIC_ASSERT_64BIT_ALIGNED( \
  672. sizeof(__instance->union_)); \
  673. size_t __member_size = PVR_STATIC_ASSERT_64BIT_ALIGNED( \
  674. sizeof(__instance->union_.member_)); \
  675. pvr_ioctl_union_padding_check(__instance, __union_offset, \
  676. __union_size, __member_size); \
  677. })
  678. /*
  679. * These utility functions should more properly be placed in pvr_fw.h, but that
  680. * would cause a dependency cycle between that header and this one. Since
  681. * they're primarily used in pvr_device.c, let's put them in here for now.
  682. */
  683. static __always_inline bool
  684. pvr_fw_irq_pending(struct pvr_device *pvr_dev)
  685. {
  686. return pvr_dev->fw_dev.defs->irq_pending(pvr_dev);
  687. }
  688. static __always_inline void
  689. pvr_fw_irq_clear(struct pvr_device *pvr_dev)
  690. {
  691. pvr_dev->fw_dev.defs->irq_clear(pvr_dev);
  692. }
  693. #endif /* PVR_DEVICE_H */