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_session.h"
12 #include "intel_pxp_tee.h"
13 #include "intel_pxp_tee_interface.h"
14
i915_dev_to_pxp(struct device * i915_kdev)15 static inline struct intel_pxp *i915_dev_to_pxp(struct device *i915_kdev)
16 {
17 return &kdev_to_i915(i915_kdev)->gt.pxp;
18 }
19
intel_pxp_tee_io_message(struct intel_pxp * pxp,void * msg_in,u32 msg_in_size,void * msg_out,u32 msg_out_max_size,u32 * msg_out_rcv_size)20 static int intel_pxp_tee_io_message(struct intel_pxp *pxp,
21 void *msg_in, u32 msg_in_size,
22 void *msg_out, u32 msg_out_max_size,
23 u32 *msg_out_rcv_size)
24 {
25 struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
26 struct i915_pxp_component *pxp_component = pxp->pxp_component;
27 int ret = 0;
28
29 mutex_lock(&pxp->tee_mutex);
30
31 /*
32 * The binding of the component is asynchronous from i915 probe, so we
33 * can't be sure it has happened.
34 */
35 if (!pxp_component) {
36 ret = -ENODEV;
37 goto unlock;
38 }
39
40 ret = pxp_component->ops->send(pxp_component->tee_dev, msg_in, msg_in_size);
41 if (ret) {
42 drm_err(&i915->drm, "Failed to send PXP TEE message\n");
43 goto unlock;
44 }
45
46 ret = pxp_component->ops->recv(pxp_component->tee_dev, msg_out, msg_out_max_size);
47 if (ret < 0) {
48 drm_err(&i915->drm, "Failed to receive PXP TEE message\n");
49 goto unlock;
50 }
51
52 if (ret > msg_out_max_size) {
53 drm_err(&i915->drm,
54 "Failed to receive PXP TEE message due to unexpected output size\n");
55 ret = -ENOSPC;
56 goto unlock;
57 }
58
59 if (msg_out_rcv_size)
60 *msg_out_rcv_size = ret;
61
62 ret = 0;
63 unlock:
64 mutex_unlock(&pxp->tee_mutex);
65 return ret;
66 }
67
68 /**
69 * i915_pxp_tee_component_bind - bind function to pass the function pointers to pxp_tee
70 * @i915_kdev: pointer to i915 kernel device
71 * @tee_kdev: pointer to tee kernel device
72 * @data: pointer to pxp_tee_master containing the function pointers
73 *
74 * This bind function is called during the system boot or resume from system sleep.
75 *
76 * Return: return 0 if successful.
77 */
i915_pxp_tee_component_bind(struct device * i915_kdev,struct device * tee_kdev,void * data)78 static int i915_pxp_tee_component_bind(struct device *i915_kdev,
79 struct device *tee_kdev, void *data)
80 {
81 struct drm_i915_private *i915 = kdev_to_i915(i915_kdev);
82 struct intel_pxp *pxp = i915_dev_to_pxp(i915_kdev);
83 intel_wakeref_t wakeref;
84
85 mutex_lock(&pxp->tee_mutex);
86 pxp->pxp_component = data;
87 pxp->pxp_component->tee_dev = tee_kdev;
88 mutex_unlock(&pxp->tee_mutex);
89
90 /* if we are suspended, the HW will be re-initialized on resume */
91 wakeref = intel_runtime_pm_get_if_in_use(&i915->runtime_pm);
92 if (!wakeref)
93 return 0;
94
95 /* the component is required to fully start the PXP HW */
96 intel_pxp_init_hw(pxp);
97
98 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
99
100 return 0;
101 }
102
i915_pxp_tee_component_unbind(struct device * i915_kdev,struct device * tee_kdev,void * data)103 static void i915_pxp_tee_component_unbind(struct device *i915_kdev,
104 struct device *tee_kdev, void *data)
105 {
106 struct intel_pxp *pxp = i915_dev_to_pxp(i915_kdev);
107
108 intel_pxp_fini_hw(pxp);
109
110 mutex_lock(&pxp->tee_mutex);
111 pxp->pxp_component = NULL;
112 mutex_unlock(&pxp->tee_mutex);
113 }
114
115 static const struct component_ops i915_pxp_tee_component_ops = {
116 .bind = i915_pxp_tee_component_bind,
117 .unbind = i915_pxp_tee_component_unbind,
118 };
119
intel_pxp_tee_component_init(struct intel_pxp * pxp)120 int intel_pxp_tee_component_init(struct intel_pxp *pxp)
121 {
122 int ret;
123 struct intel_gt *gt = pxp_to_gt(pxp);
124 struct drm_i915_private *i915 = gt->i915;
125
126 ret = component_add_typed(i915->drm.dev, &i915_pxp_tee_component_ops,
127 I915_COMPONENT_PXP);
128 if (ret < 0) {
129 drm_err(&i915->drm, "Failed to add PXP component (%d)\n", ret);
130 return ret;
131 }
132
133 pxp->pxp_component_added = true;
134
135 return 0;
136 }
137
intel_pxp_tee_component_fini(struct intel_pxp * pxp)138 void intel_pxp_tee_component_fini(struct intel_pxp *pxp)
139 {
140 struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
141
142 if (!pxp->pxp_component_added)
143 return;
144
145 component_del(i915->drm.dev, &i915_pxp_tee_component_ops);
146 pxp->pxp_component_added = false;
147 }
148
intel_pxp_tee_cmd_create_arb_session(struct intel_pxp * pxp,int arb_session_id)149 int intel_pxp_tee_cmd_create_arb_session(struct intel_pxp *pxp,
150 int arb_session_id)
151 {
152 struct drm_i915_private *i915 = pxp_to_gt(pxp)->i915;
153 struct pxp_tee_create_arb_in msg_in = {0};
154 struct pxp_tee_create_arb_out msg_out = {0};
155 int ret;
156
157 msg_in.header.api_version = PXP_TEE_APIVER;
158 msg_in.header.command_id = PXP_TEE_ARB_CMDID;
159 msg_in.header.buffer_len = sizeof(msg_in) - sizeof(msg_in.header);
160 msg_in.protection_mode = PXP_TEE_ARB_PROTECTION_MODE;
161 msg_in.session_id = arb_session_id;
162
163 ret = intel_pxp_tee_io_message(pxp,
164 &msg_in, sizeof(msg_in),
165 &msg_out, sizeof(msg_out),
166 NULL);
167
168 if (ret)
169 drm_err(&i915->drm, "Failed to send tee msg ret=[%d]\n", ret);
170
171 return ret;
172 }
173