1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright(c) 2020 Intel Corporation.
4  */
5 
6 #include <linux/component.h>
7 #include "drm/i915_pxp_tee_interface.h"
8 #include "drm/i915_component.h"
9 #include "i915_drv.h"
10 #include "intel_pxp.h"
11 #include "intel_pxp_tee.h"
12 
13 static inline struct intel_pxp *i915_dev_to_pxp(struct device *i915_kdev)
14 {
15 	return &kdev_to_i915(i915_kdev)->gt.pxp;
16 }
17 
18 /**
19  * i915_pxp_tee_component_bind - bind function to pass the function pointers to pxp_tee
20  * @i915_kdev: pointer to i915 kernel device
21  * @tee_kdev: pointer to tee kernel device
22  * @data: pointer to pxp_tee_master containing the function pointers
23  *
24  * This bind function is called during the system boot or resume from system sleep.
25  *
26  * Return: return 0 if successful.
27  */
28 static int i915_pxp_tee_component_bind(struct device *i915_kdev,
29 				       struct device *tee_kdev, void *data)
30 {
31 	struct intel_pxp *pxp = i915_dev_to_pxp(i915_kdev);
32 
33 	pxp->pxp_component = data;
34 	pxp->pxp_component->tee_dev = tee_kdev;
35 
36 	return 0;
37 }
38 
39 static void i915_pxp_tee_component_unbind(struct device *i915_kdev,
40 					  struct device *tee_kdev, void *data)
41 {
42 	struct intel_pxp *pxp = i915_dev_to_pxp(i915_kdev);
43 
44 	pxp->pxp_component = NULL;
45 }
46 
47 static const struct component_ops i915_pxp_tee_component_ops = {
48 	.bind   = i915_pxp_tee_component_bind,
49 	.unbind = i915_pxp_tee_component_unbind,
50 };
51 
52 int intel_pxp_tee_component_init(struct intel_pxp *pxp)
53 {
54 	int ret;
55 	struct intel_gt *gt = pxp_to_gt(pxp);
56 	struct drm_i915_private *i915 = gt->i915;
57 
58 	ret = component_add_typed(i915->drm.dev, &i915_pxp_tee_component_ops,
59 				  I915_COMPONENT_PXP);
60 	if (ret < 0) {
61 		drm_err(&i915->drm, "Failed to add PXP component (%d)\n", ret);
62 		return ret;
63 	}
64 
65 	pxp->pxp_component_added = true;
66 
67 	return 0;
68 }
69 
70 void intel_pxp_tee_component_fini(struct intel_pxp *pxp)
71 {
72 	struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
73 
74 	if (!pxp->pxp_component_added)
75 		return;
76 
77 	component_del(i915->drm.dev, &i915_pxp_tee_component_ops);
78 	pxp->pxp_component_added = false;
79 }
80