Commit f091a3d4 authored by Francisco Jerez's avatar Francisco Jerez Committed by Ben Skeggs

drm/nouveau: Implement weak channel references.

nouveau_channel_ref() takes a "weak" channel reference that doesn't
prevent the hardware channel resources from being released, it just
keeps the channel data structure alive.
Signed-off-by: default avatarFrancisco Jerez <currojerez@riseup.net>
Signed-off-by: default avatarBen Skeggs <bskeggs@redhat.com>
parent 36c952e8
...@@ -125,7 +125,8 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret, ...@@ -125,7 +125,8 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
chan->vram_handle = vram_handle; chan->vram_handle = vram_handle;
chan->gart_handle = gart_handle; chan->gart_handle = gart_handle;
atomic_set(&chan->refcount, 1); kref_init(&chan->ref);
atomic_set(&chan->users, 1);
mutex_init(&chan->mutex); mutex_init(&chan->mutex);
mutex_lock(&chan->mutex); mutex_lock(&chan->mutex);
...@@ -133,7 +134,7 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret, ...@@ -133,7 +134,7 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
spin_lock_irqsave(&dev_priv->channels.lock, flags); spin_lock_irqsave(&dev_priv->channels.lock, flags);
for (chan->id = 0; chan->id < pfifo->channels; chan->id++) { for (chan->id = 0; chan->id < pfifo->channels; chan->id++) {
if (!dev_priv->channels.ptr[chan->id]) { if (!dev_priv->channels.ptr[chan->id]) {
dev_priv->channels.ptr[chan->id] = chan; nouveau_channel_ref(chan, &dev_priv->channels.ptr[chan->id]);
break; break;
} }
} }
...@@ -240,10 +241,12 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret, ...@@ -240,10 +241,12 @@ nouveau_channel_alloc(struct drm_device *dev, struct nouveau_channel **chan_ret,
struct nouveau_channel * struct nouveau_channel *
nouveau_channel_get_unlocked(struct nouveau_channel *ref) nouveau_channel_get_unlocked(struct nouveau_channel *ref)
{ {
if (likely(ref && atomic_inc_not_zero(&ref->refcount))) struct nouveau_channel *chan = NULL;
return ref;
return NULL; if (likely(ref && atomic_inc_not_zero(&ref->users)))
nouveau_channel_ref(ref, &chan);
return chan;
} }
struct nouveau_channel * struct nouveau_channel *
...@@ -281,15 +284,14 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan) ...@@ -281,15 +284,14 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
int ret; int ret;
/* decrement the refcount, and we're done if there's still refs */ /* decrement the refcount, and we're done if there's still refs */
if (likely(!atomic_dec_and_test(&chan->refcount))) { if (likely(!atomic_dec_and_test(&chan->users))) {
*pchan = NULL; nouveau_channel_ref(NULL, pchan);
return; return;
} }
/* noone wants the channel anymore */ /* noone wants the channel anymore */
NV_DEBUG(dev, "freeing channel %d\n", chan->id); NV_DEBUG(dev, "freeing channel %d\n", chan->id);
nouveau_debugfs_channel_fini(chan); nouveau_debugfs_channel_fini(chan);
*pchan = NULL;
/* give it chance to idle */ /* give it chance to idle */
nouveau_fence_update(chan); nouveau_fence_update(chan);
...@@ -333,7 +335,7 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan) ...@@ -333,7 +335,7 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
* remove it from the channel list * remove it from the channel list
*/ */
spin_lock_irqsave(&dev_priv->channels.lock, flags); spin_lock_irqsave(&dev_priv->channels.lock, flags);
dev_priv->channels.ptr[chan->id] = NULL; nouveau_channel_ref(NULL, &dev_priv->channels.ptr[chan->id]);
spin_unlock_irqrestore(&dev_priv->channels.lock, flags); spin_unlock_irqrestore(&dev_priv->channels.lock, flags);
/* destroy any resources the channel owned */ /* destroy any resources the channel owned */
...@@ -345,10 +347,8 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan) ...@@ -345,10 +347,8 @@ nouveau_channel_put_unlocked(struct nouveau_channel **pchan)
} }
nouveau_gpuobj_channel_takedown(chan); nouveau_gpuobj_channel_takedown(chan);
nouveau_notifier_takedown_channel(chan); nouveau_notifier_takedown_channel(chan);
if (chan->user)
iounmap(chan->user);
kfree(chan); nouveau_channel_ref(NULL, pchan);
} }
void void
...@@ -358,6 +358,31 @@ nouveau_channel_put(struct nouveau_channel **pchan) ...@@ -358,6 +358,31 @@ nouveau_channel_put(struct nouveau_channel **pchan)
nouveau_channel_put_unlocked(pchan); nouveau_channel_put_unlocked(pchan);
} }
static void
nouveau_channel_del(struct kref *ref)
{
struct nouveau_channel *chan =
container_of(ref, struct nouveau_channel, ref);
if (chan->user)
iounmap(chan->user);
kfree(chan);
}
void
nouveau_channel_ref(struct nouveau_channel *chan,
struct nouveau_channel **pchan)
{
if (chan)
kref_get(&chan->ref);
if (*pchan)
kref_put(&(*pchan)->ref, nouveau_channel_del);
*pchan = chan;
}
/* cleans up all the fifos from file_priv */ /* cleans up all the fifos from file_priv */
void void
nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv) nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
...@@ -373,7 +398,7 @@ nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv) ...@@ -373,7 +398,7 @@ nouveau_channel_cleanup(struct drm_device *dev, struct drm_file *file_priv)
if (IS_ERR(chan)) if (IS_ERR(chan))
continue; continue;
atomic_dec(&chan->refcount); atomic_dec(&chan->users);
nouveau_channel_put(&chan); nouveau_channel_put(&chan);
} }
} }
...@@ -427,7 +452,7 @@ nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data, ...@@ -427,7 +452,7 @@ nouveau_ioctl_fifo_alloc(struct drm_device *dev, void *data,
&init->notifier_handle); &init->notifier_handle);
if (ret == 0) if (ret == 0)
atomic_inc(&chan->refcount); /* userspace reference */ atomic_inc(&chan->users); /* userspace reference */
nouveau_channel_put(&chan); nouveau_channel_put(&chan);
return ret; return ret;
} }
...@@ -443,7 +468,7 @@ nouveau_ioctl_fifo_free(struct drm_device *dev, void *data, ...@@ -443,7 +468,7 @@ nouveau_ioctl_fifo_free(struct drm_device *dev, void *data,
if (IS_ERR(chan)) if (IS_ERR(chan))
return PTR_ERR(chan); return PTR_ERR(chan);
atomic_dec(&chan->refcount); atomic_dec(&chan->users);
nouveau_channel_put(&chan); nouveau_channel_put(&chan);
return 0; return 0;
} }
......
...@@ -165,7 +165,11 @@ struct nouveau_channel { ...@@ -165,7 +165,11 @@ struct nouveau_channel {
struct drm_device *dev; struct drm_device *dev;
int id; int id;
atomic_t refcount; /* references to the channel data structure */
struct kref ref;
/* users of the hardware channel resources, the hardware
* context will be kicked off when it reaches zero. */
atomic_t users;
struct mutex mutex; struct mutex mutex;
/* owner of this fifo */ /* owner of this fifo */
...@@ -808,6 +812,8 @@ extern struct nouveau_channel * ...@@ -808,6 +812,8 @@ extern struct nouveau_channel *
nouveau_channel_get(struct drm_device *, struct drm_file *, int id); nouveau_channel_get(struct drm_device *, struct drm_file *, int id);
extern void nouveau_channel_put_unlocked(struct nouveau_channel **); extern void nouveau_channel_put_unlocked(struct nouveau_channel **);
extern void nouveau_channel_put(struct nouveau_channel **); extern void nouveau_channel_put(struct nouveau_channel **);
extern void nouveau_channel_ref(struct nouveau_channel *chan,
struct nouveau_channel **pchan);
/* nouveau_object.c */ /* nouveau_object.c */
extern int nouveau_gpuobj_early_init(struct drm_device *); extern int nouveau_gpuobj_early_init(struct drm_device *);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment