88
99#include <linux/debugfs.h>
1010#include <linux/device.h>
11- #include <linux/idr .h>
11+ #include <linux/xarray .h>
1212
1313#include <drm/drm_accel.h>
1414#include <drm/drm_auth.h>
1818#include <drm/drm_ioctl.h>
1919#include <drm/drm_print.h>
2020
21- static DEFINE_SPINLOCK (accel_minor_lock );
22- static struct idr accel_minors_idr ;
21+ DEFINE_XARRAY_ALLOC (accel_minors_xa );
2322
2423static struct dentry * accel_debugfs_root ;
2524
@@ -117,99 +116,6 @@ void accel_set_device_instance_params(struct device *kdev, int index)
117116 kdev -> type = & accel_sysfs_device_minor ;
118117}
119118
120- /**
121- * accel_minor_alloc() - Allocates a new accel minor
122- *
123- * This function access the accel minors idr and allocates from it
124- * a new id to represent a new accel minor
125- *
126- * Return: A new id on success or error code in case idr_alloc failed
127- */
128- int accel_minor_alloc (void )
129- {
130- unsigned long flags ;
131- int r ;
132-
133- spin_lock_irqsave (& accel_minor_lock , flags );
134- r = idr_alloc (& accel_minors_idr , NULL , 0 , ACCEL_MAX_MINORS , GFP_NOWAIT );
135- spin_unlock_irqrestore (& accel_minor_lock , flags );
136-
137- return r ;
138- }
139-
140- /**
141- * accel_minor_remove() - Remove an accel minor
142- * @index: The minor id to remove.
143- *
144- * This function access the accel minors idr and removes from
145- * it the member with the id that is passed to this function.
146- */
147- void accel_minor_remove (int index )
148- {
149- unsigned long flags ;
150-
151- spin_lock_irqsave (& accel_minor_lock , flags );
152- idr_remove (& accel_minors_idr , index );
153- spin_unlock_irqrestore (& accel_minor_lock , flags );
154- }
155-
156- /**
157- * accel_minor_replace() - Replace minor pointer in accel minors idr.
158- * @minor: Pointer to the new minor.
159- * @index: The minor id to replace.
160- *
161- * This function access the accel minors idr structure and replaces the pointer
162- * that is associated with an existing id. Because the minor pointer can be
163- * NULL, we need to explicitly pass the index.
164- *
165- * Return: 0 for success, negative value for error
166- */
167- void accel_minor_replace (struct drm_minor * minor , int index )
168- {
169- unsigned long flags ;
170-
171- spin_lock_irqsave (& accel_minor_lock , flags );
172- idr_replace (& accel_minors_idr , minor , index );
173- spin_unlock_irqrestore (& accel_minor_lock , flags );
174- }
175-
176- /*
177- * Looks up the given minor-ID and returns the respective DRM-minor object. The
178- * refence-count of the underlying device is increased so you must release this
179- * object with accel_minor_release().
180- *
181- * The object can be only a drm_minor that represents an accel device.
182- *
183- * As long as you hold this minor, it is guaranteed that the object and the
184- * minor->dev pointer will stay valid! However, the device may get unplugged and
185- * unregistered while you hold the minor.
186- */
187- static struct drm_minor * accel_minor_acquire (unsigned int minor_id )
188- {
189- struct drm_minor * minor ;
190- unsigned long flags ;
191-
192- spin_lock_irqsave (& accel_minor_lock , flags );
193- minor = idr_find (& accel_minors_idr , minor_id );
194- if (minor )
195- drm_dev_get (minor -> dev );
196- spin_unlock_irqrestore (& accel_minor_lock , flags );
197-
198- if (!minor ) {
199- return ERR_PTR (- ENODEV );
200- } else if (drm_dev_is_unplugged (minor -> dev )) {
201- drm_dev_put (minor -> dev );
202- return ERR_PTR (- ENODEV );
203- }
204-
205- return minor ;
206- }
207-
208- static void accel_minor_release (struct drm_minor * minor )
209- {
210- drm_dev_put (minor -> dev );
211- }
212-
213119/**
214120 * accel_open - open method for ACCEL file
215121 * @inode: device inode
@@ -227,7 +133,7 @@ int accel_open(struct inode *inode, struct file *filp)
227133 struct drm_minor * minor ;
228134 int retcode ;
229135
230- minor = accel_minor_acquire ( iminor (inode ));
136+ minor = drm_minor_acquire ( & accel_minors_xa , iminor (inode ));
231137 if (IS_ERR (minor ))
232138 return PTR_ERR (minor );
233139
@@ -246,7 +152,7 @@ int accel_open(struct inode *inode, struct file *filp)
246152
247153err_undo :
248154 atomic_dec (& dev -> open_count );
249- accel_minor_release (minor );
155+ drm_minor_release (minor );
250156 return retcode ;
251157}
252158EXPORT_SYMBOL_GPL (accel_open );
@@ -257,7 +163,7 @@ static int accel_stub_open(struct inode *inode, struct file *filp)
257163 struct drm_minor * minor ;
258164 int err ;
259165
260- minor = accel_minor_acquire ( iminor (inode ));
166+ minor = drm_minor_acquire ( & accel_minors_xa , iminor (inode ));
261167 if (IS_ERR (minor ))
262168 return PTR_ERR (minor );
263169
@@ -274,7 +180,7 @@ static int accel_stub_open(struct inode *inode, struct file *filp)
274180 err = 0 ;
275181
276182out :
277- accel_minor_release (minor );
183+ drm_minor_release (minor );
278184
279185 return err ;
280186}
@@ -290,15 +196,13 @@ void accel_core_exit(void)
290196 unregister_chrdev (ACCEL_MAJOR , "accel" );
291197 debugfs_remove (accel_debugfs_root );
292198 accel_sysfs_destroy ();
293- idr_destroy ( & accel_minors_idr );
199+ WARN_ON (! xa_empty ( & accel_minors_xa ) );
294200}
295201
296202int __init accel_core_init (void )
297203{
298204 int ret ;
299205
300- idr_init (& accel_minors_idr );
301-
302206 ret = accel_sysfs_init ();
303207 if (ret < 0 ) {
304208 DRM_ERROR ("Cannot create ACCEL class: %d\n" , ret );
0 commit comments