i915_gem.c 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227
  1. /*
  2. * Copyright © 2008 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * Eric Anholt <eric@anholt.net>
  25. *
  26. */
  27. #include <drm/drmP.h>
  28. #include <drm/drm_vma_manager.h>
  29. #include <drm/i915_drm.h>
  30. #include "i915_drv.h"
  31. #include "i915_trace.h"
  32. #include "intel_drv.h"
  33. #include <linux/oom.h>
  34. #include <linux/shmem_fs.h>
  35. #include <linux/slab.h>
  36. #include <linux/swap.h>
  37. #include <linux/pci.h>
  38. #include <linux/dma-buf.h>
  39. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  40. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  41. bool force);
  42. static __must_check int
  43. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  44. bool readonly);
  45. static void
  46. i915_gem_object_retire(struct drm_i915_gem_object *obj);
  47. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  48. struct drm_i915_gem_object *obj);
  49. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  50. struct drm_i915_fence_reg *fence,
  51. bool enable);
  52. static unsigned long i915_gem_shrinker_count(struct shrinker *shrinker,
  53. struct shrink_control *sc);
  54. static unsigned long i915_gem_shrinker_scan(struct shrinker *shrinker,
  55. struct shrink_control *sc);
  56. static int i915_gem_shrinker_oom(struct notifier_block *nb,
  57. unsigned long event,
  58. void *ptr);
  59. static unsigned long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
  60. static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  61. static bool cpu_cache_is_coherent(struct drm_device *dev,
  62. enum i915_cache_level level)
  63. {
  64. return HAS_LLC(dev) || level != I915_CACHE_NONE;
  65. }
  66. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  67. {
  68. if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  69. return true;
  70. return obj->pin_display;
  71. }
  72. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  73. {
  74. if (obj->tiling_mode)
  75. i915_gem_release_mmap(obj);
  76. /* As we do not have an associated fence register, we will force
  77. * a tiling change if we ever need to acquire one.
  78. */
  79. obj->fence_dirty = false;
  80. obj->fence_reg = I915_FENCE_REG_NONE;
  81. }
  82. /* some bookkeeping */
  83. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  84. size_t size)
  85. {
  86. spin_lock(&dev_priv->mm.object_stat_lock);
  87. dev_priv->mm.object_count++;
  88. dev_priv->mm.object_memory += size;
  89. spin_unlock(&dev_priv->mm.object_stat_lock);
  90. }
  91. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  92. size_t size)
  93. {
  94. spin_lock(&dev_priv->mm.object_stat_lock);
  95. dev_priv->mm.object_count--;
  96. dev_priv->mm.object_memory -= size;
  97. spin_unlock(&dev_priv->mm.object_stat_lock);
  98. }
  99. static int
  100. i915_gem_wait_for_error(struct i915_gpu_error *error)
  101. {
  102. int ret;
  103. #define EXIT_COND (!i915_reset_in_progress(error) || \
  104. i915_terminally_wedged(error))
  105. if (EXIT_COND)
  106. return 0;
  107. /*
  108. * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  109. * userspace. If it takes that long something really bad is going on and
  110. * we should simply try to bail out and fail as gracefully as possible.
  111. */
  112. ret = wait_event_interruptible_timeout(error->reset_queue,
  113. EXIT_COND,
  114. 10*HZ);
  115. if (ret == 0) {
  116. DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  117. return -EIO;
  118. } else if (ret < 0) {
  119. return ret;
  120. }
  121. #undef EXIT_COND
  122. return 0;
  123. }
  124. int i915_mutex_lock_interruptible(struct drm_device *dev)
  125. {
  126. struct drm_i915_private *dev_priv = dev->dev_private;
  127. int ret;
  128. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  129. if (ret)
  130. return ret;
  131. ret = mutex_lock_interruptible(&dev->struct_mutex);
  132. if (ret)
  133. return ret;
  134. WARN_ON(i915_verify_lists(dev));
  135. return 0;
  136. }
  137. static inline bool
  138. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  139. {
  140. return i915_gem_obj_bound_any(obj) && !obj->active;
  141. }
  142. int
  143. i915_gem_init_ioctl(struct drm_device *dev, void *data,
  144. struct drm_file *file)
  145. {
  146. struct drm_i915_private *dev_priv = dev->dev_private;
  147. struct drm_i915_gem_init *args = data;
  148. if (drm_core_check_feature(dev, DRIVER_MODESET))
  149. return -ENODEV;
  150. if (args->gtt_start >= args->gtt_end ||
  151. (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
  152. return -EINVAL;
  153. /* GEM with user mode setting was never supported on ilk and later. */
  154. if (INTEL_INFO(dev)->gen >= 5)
  155. return -ENODEV;
  156. mutex_lock(&dev->struct_mutex);
  157. i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
  158. args->gtt_end);
  159. dev_priv->gtt.mappable_end = args->gtt_end;
  160. mutex_unlock(&dev->struct_mutex);
  161. return 0;
  162. }
  163. int
  164. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  165. struct drm_file *file)
  166. {
  167. struct drm_i915_private *dev_priv = dev->dev_private;
  168. struct drm_i915_gem_get_aperture *args = data;
  169. struct drm_i915_gem_object *obj;
  170. size_t pinned;
  171. pinned = 0;
  172. mutex_lock(&dev->struct_mutex);
  173. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  174. if (i915_gem_obj_is_pinned(obj))
  175. pinned += i915_gem_obj_ggtt_size(obj);
  176. mutex_unlock(&dev->struct_mutex);
  177. args->aper_size = dev_priv->gtt.base.total;
  178. args->aper_available_size = args->aper_size - pinned;
  179. return 0;
  180. }
  181. static void i915_gem_object_detach_phys(struct drm_i915_gem_object *obj)
  182. {
  183. drm_dma_handle_t *phys = obj->phys_handle;
  184. if (!phys)
  185. return;
  186. if (obj->madv == I915_MADV_WILLNEED) {
  187. struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
  188. char *vaddr = phys->vaddr;
  189. int i;
  190. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  191. struct page *page = shmem_read_mapping_page(mapping, i);
  192. if (!IS_ERR(page)) {
  193. char *dst = kmap_atomic(page);
  194. memcpy(dst, vaddr, PAGE_SIZE);
  195. drm_clflush_virt_range(dst, PAGE_SIZE);
  196. kunmap_atomic(dst);
  197. set_page_dirty(page);
  198. mark_page_accessed(page);
  199. page_cache_release(page);
  200. }
  201. vaddr += PAGE_SIZE;
  202. }
  203. i915_gem_chipset_flush(obj->base.dev);
  204. }
  205. #ifdef CONFIG_X86
  206. set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
  207. #endif
  208. drm_pci_free(obj->base.dev, phys);
  209. obj->phys_handle = NULL;
  210. }
  211. int
  212. i915_gem_object_attach_phys(struct drm_i915_gem_object *obj,
  213. int align)
  214. {
  215. drm_dma_handle_t *phys;
  216. struct address_space *mapping;
  217. char *vaddr;
  218. int i;
  219. if (obj->phys_handle) {
  220. if ((unsigned long)obj->phys_handle->vaddr & (align -1))
  221. return -EBUSY;
  222. return 0;
  223. }
  224. if (obj->madv != I915_MADV_WILLNEED)
  225. return -EFAULT;
  226. if (obj->base.filp == NULL)
  227. return -EINVAL;
  228. /* create a new object */
  229. phys = drm_pci_alloc(obj->base.dev, obj->base.size, align);
  230. if (!phys)
  231. return -ENOMEM;
  232. vaddr = phys->vaddr;
  233. #ifdef CONFIG_X86
  234. set_memory_wc((unsigned long)vaddr, phys->size / PAGE_SIZE);
  235. #endif
  236. mapping = file_inode(obj->base.filp)->i_mapping;
  237. for (i = 0; i < obj->base.size / PAGE_SIZE; i++) {
  238. struct page *page;
  239. char *src;
  240. page = shmem_read_mapping_page(mapping, i);
  241. if (IS_ERR(page)) {
  242. #ifdef CONFIG_X86
  243. set_memory_wb((unsigned long)phys->vaddr, phys->size / PAGE_SIZE);
  244. #endif
  245. drm_pci_free(obj->base.dev, phys);
  246. return PTR_ERR(page);
  247. }
  248. src = kmap_atomic(page);
  249. memcpy(vaddr, src, PAGE_SIZE);
  250. kunmap_atomic(src);
  251. mark_page_accessed(page);
  252. page_cache_release(page);
  253. vaddr += PAGE_SIZE;
  254. }
  255. obj->phys_handle = phys;
  256. return 0;
  257. }
  258. static int
  259. i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
  260. struct drm_i915_gem_pwrite *args,
  261. struct drm_file *file_priv)
  262. {
  263. struct drm_device *dev = obj->base.dev;
  264. void *vaddr = obj->phys_handle->vaddr + args->offset;
  265. char __user *user_data = to_user_ptr(args->data_ptr);
  266. if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
  267. unsigned long unwritten;
  268. /* The physical object once assigned is fixed for the lifetime
  269. * of the obj, so we can safely drop the lock and continue
  270. * to access vaddr.
  271. */
  272. mutex_unlock(&dev->struct_mutex);
  273. unwritten = copy_from_user(vaddr, user_data, args->size);
  274. mutex_lock(&dev->struct_mutex);
  275. if (unwritten)
  276. return -EFAULT;
  277. }
  278. i915_gem_chipset_flush(dev);
  279. return 0;
  280. }
  281. void *i915_gem_object_alloc(struct drm_device *dev)
  282. {
  283. struct drm_i915_private *dev_priv = dev->dev_private;
  284. return kmem_cache_zalloc(dev_priv->slab, GFP_KERNEL);
  285. }
  286. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  287. {
  288. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  289. kmem_cache_free(dev_priv->slab, obj);
  290. }
  291. static int
  292. i915_gem_create(struct drm_file *file,
  293. struct drm_device *dev,
  294. uint64_t size,
  295. uint32_t *handle_p)
  296. {
  297. struct drm_i915_gem_object *obj;
  298. int ret;
  299. u32 handle;
  300. size = roundup(size, PAGE_SIZE);
  301. if (size == 0)
  302. return -EINVAL;
  303. /* Allocate the new object */
  304. obj = i915_gem_alloc_object(dev, size);
  305. if (obj == NULL)
  306. return -ENOMEM;
  307. ret = drm_gem_handle_create(file, &obj->base, &handle);
  308. /* drop reference from allocate - handle holds it now */
  309. drm_gem_object_unreference_unlocked(&obj->base);
  310. if (ret)
  311. return ret;
  312. *handle_p = handle;
  313. return 0;
  314. }
  315. int
  316. i915_gem_dumb_create(struct drm_file *file,
  317. struct drm_device *dev,
  318. struct drm_mode_create_dumb *args)
  319. {
  320. /* have to work out size/pitch and return them */
  321. args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  322. args->size = args->pitch * args->height;
  323. return i915_gem_create(file, dev,
  324. args->size, &args->handle);
  325. }
  326. /**
  327. * Creates a new mm object and returns a handle to it.
  328. */
  329. int
  330. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  331. struct drm_file *file)
  332. {
  333. struct drm_i915_gem_create *args = data;
  334. return i915_gem_create(file, dev,
  335. args->size, &args->handle);
  336. }
  337. static inline int
  338. __copy_to_user_swizzled(char __user *cpu_vaddr,
  339. const char *gpu_vaddr, int gpu_offset,
  340. int length)
  341. {
  342. int ret, cpu_offset = 0;
  343. while (length > 0) {
  344. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  345. int this_length = min(cacheline_end - gpu_offset, length);
  346. int swizzled_gpu_offset = gpu_offset ^ 64;
  347. ret = __copy_to_user(cpu_vaddr + cpu_offset,
  348. gpu_vaddr + swizzled_gpu_offset,
  349. this_length);
  350. if (ret)
  351. return ret + length;
  352. cpu_offset += this_length;
  353. gpu_offset += this_length;
  354. length -= this_length;
  355. }
  356. return 0;
  357. }
  358. static inline int
  359. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  360. const char __user *cpu_vaddr,
  361. int length)
  362. {
  363. int ret, cpu_offset = 0;
  364. while (length > 0) {
  365. int cacheline_end = ALIGN(gpu_offset + 1, 64);
  366. int this_length = min(cacheline_end - gpu_offset, length);
  367. int swizzled_gpu_offset = gpu_offset ^ 64;
  368. ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  369. cpu_vaddr + cpu_offset,
  370. this_length);
  371. if (ret)
  372. return ret + length;
  373. cpu_offset += this_length;
  374. gpu_offset += this_length;
  375. length -= this_length;
  376. }
  377. return 0;
  378. }
  379. /*
  380. * Pins the specified object's pages and synchronizes the object with
  381. * GPU accesses. Sets needs_clflush to non-zero if the caller should
  382. * flush the object from the CPU cache.
  383. */
  384. int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object *obj,
  385. int *needs_clflush)
  386. {
  387. int ret;
  388. *needs_clflush = 0;
  389. if (!obj->base.filp)
  390. return -EINVAL;
  391. if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
  392. /* If we're not in the cpu read domain, set ourself into the gtt
  393. * read domain and manually flush cachelines (if required). This
  394. * optimizes for the case when the gpu will dirty the data
  395. * anyway again before the next pread happens. */
  396. *needs_clflush = !cpu_cache_is_coherent(obj->base.dev,
  397. obj->cache_level);
  398. ret = i915_gem_object_wait_rendering(obj, true);
  399. if (ret)
  400. return ret;
  401. i915_gem_object_retire(obj);
  402. }
  403. ret = i915_gem_object_get_pages(obj);
  404. if (ret)
  405. return ret;
  406. i915_gem_object_pin_pages(obj);
  407. return ret;
  408. }
  409. /* Per-page copy function for the shmem pread fastpath.
  410. * Flushes invalid cachelines before reading the target if
  411. * needs_clflush is set. */
  412. static int
  413. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  414. char __user *user_data,
  415. bool page_do_bit17_swizzling, bool needs_clflush)
  416. {
  417. char *vaddr;
  418. int ret;
  419. if (unlikely(page_do_bit17_swizzling))
  420. return -EINVAL;
  421. vaddr = kmap_atomic(page);
  422. if (needs_clflush)
  423. drm_clflush_virt_range(vaddr + shmem_page_offset,
  424. page_length);
  425. ret = __copy_to_user_inatomic(user_data,
  426. vaddr + shmem_page_offset,
  427. page_length);
  428. kunmap_atomic(vaddr);
  429. return ret ? -EFAULT : 0;
  430. }
  431. static void
  432. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  433. bool swizzled)
  434. {
  435. if (unlikely(swizzled)) {
  436. unsigned long start = (unsigned long) addr;
  437. unsigned long end = (unsigned long) addr + length;
  438. /* For swizzling simply ensure that we always flush both
  439. * channels. Lame, but simple and it works. Swizzled
  440. * pwrite/pread is far from a hotpath - current userspace
  441. * doesn't use it at all. */
  442. start = round_down(start, 128);
  443. end = round_up(end, 128);
  444. drm_clflush_virt_range((void *)start, end - start);
  445. } else {
  446. drm_clflush_virt_range(addr, length);
  447. }
  448. }
  449. /* Only difference to the fast-path function is that this can handle bit17
  450. * and uses non-atomic copy and kmap functions. */
  451. static int
  452. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  453. char __user *user_data,
  454. bool page_do_bit17_swizzling, bool needs_clflush)
  455. {
  456. char *vaddr;
  457. int ret;
  458. vaddr = kmap(page);
  459. if (needs_clflush)
  460. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  461. page_length,
  462. page_do_bit17_swizzling);
  463. if (page_do_bit17_swizzling)
  464. ret = __copy_to_user_swizzled(user_data,
  465. vaddr, shmem_page_offset,
  466. page_length);
  467. else
  468. ret = __copy_to_user(user_data,
  469. vaddr + shmem_page_offset,
  470. page_length);
  471. kunmap(page);
  472. return ret ? - EFAULT : 0;
  473. }
  474. static int
  475. i915_gem_shmem_pread(struct drm_device *dev,
  476. struct drm_i915_gem_object *obj,
  477. struct drm_i915_gem_pread *args,
  478. struct drm_file *file)
  479. {
  480. char __user *user_data;
  481. ssize_t remain;
  482. loff_t offset;
  483. int shmem_page_offset, page_length, ret = 0;
  484. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  485. int prefaulted = 0;
  486. int needs_clflush = 0;
  487. struct sg_page_iter sg_iter;
  488. user_data = to_user_ptr(args->data_ptr);
  489. remain = args->size;
  490. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  491. ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  492. if (ret)
  493. return ret;
  494. offset = args->offset;
  495. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  496. offset >> PAGE_SHIFT) {
  497. struct page *page = sg_page_iter_page(&sg_iter);
  498. if (remain <= 0)
  499. break;
  500. /* Operation in this page
  501. *
  502. * shmem_page_offset = offset within page in shmem file
  503. * page_length = bytes to copy for this page
  504. */
  505. shmem_page_offset = offset_in_page(offset);
  506. page_length = remain;
  507. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  508. page_length = PAGE_SIZE - shmem_page_offset;
  509. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  510. (page_to_phys(page) & (1 << 17)) != 0;
  511. ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  512. user_data, page_do_bit17_swizzling,
  513. needs_clflush);
  514. if (ret == 0)
  515. goto next_page;
  516. mutex_unlock(&dev->struct_mutex);
  517. if (likely(!i915.prefault_disable) && !prefaulted) {
  518. ret = fault_in_multipages_writeable(user_data, remain);
  519. /* Userspace is tricking us, but we've already clobbered
  520. * its pages with the prefault and promised to write the
  521. * data up to the first fault. Hence ignore any errors
  522. * and just continue. */
  523. (void)ret;
  524. prefaulted = 1;
  525. }
  526. ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  527. user_data, page_do_bit17_swizzling,
  528. needs_clflush);
  529. mutex_lock(&dev->struct_mutex);
  530. if (ret)
  531. goto out;
  532. next_page:
  533. remain -= page_length;
  534. user_data += page_length;
  535. offset += page_length;
  536. }
  537. out:
  538. i915_gem_object_unpin_pages(obj);
  539. return ret;
  540. }
  541. /**
  542. * Reads data from the object referenced by handle.
  543. *
  544. * On error, the contents of *data are undefined.
  545. */
  546. int
  547. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  548. struct drm_file *file)
  549. {
  550. struct drm_i915_gem_pread *args = data;
  551. struct drm_i915_gem_object *obj;
  552. int ret = 0;
  553. if (args->size == 0)
  554. return 0;
  555. if (!access_ok(VERIFY_WRITE,
  556. to_user_ptr(args->data_ptr),
  557. args->size))
  558. return -EFAULT;
  559. ret = i915_mutex_lock_interruptible(dev);
  560. if (ret)
  561. return ret;
  562. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  563. if (&obj->base == NULL) {
  564. ret = -ENOENT;
  565. goto unlock;
  566. }
  567. /* Bounds check source. */
  568. if (args->offset > obj->base.size ||
  569. args->size > obj->base.size - args->offset) {
  570. ret = -EINVAL;
  571. goto out;
  572. }
  573. /* prime objects have no backing filp to GEM pread/pwrite
  574. * pages from.
  575. */
  576. if (!obj->base.filp) {
  577. ret = -EINVAL;
  578. goto out;
  579. }
  580. trace_i915_gem_object_pread(obj, args->offset, args->size);
  581. ret = i915_gem_shmem_pread(dev, obj, args, file);
  582. out:
  583. drm_gem_object_unreference(&obj->base);
  584. unlock:
  585. mutex_unlock(&dev->struct_mutex);
  586. return ret;
  587. }
  588. /* This is the fast write path which cannot handle
  589. * page faults in the source data
  590. */
  591. static inline int
  592. fast_user_write(struct io_mapping *mapping,
  593. loff_t page_base, int page_offset,
  594. char __user *user_data,
  595. int length)
  596. {
  597. void __iomem *vaddr_atomic;
  598. void *vaddr;
  599. unsigned long unwritten;
  600. vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  601. /* We can use the cpu mem copy function because this is X86. */
  602. vaddr = (void __force*)vaddr_atomic + page_offset;
  603. unwritten = __copy_from_user_inatomic_nocache(vaddr,
  604. user_data, length);
  605. io_mapping_unmap_atomic(vaddr_atomic);
  606. return unwritten;
  607. }
  608. /**
  609. * This is the fast pwrite path, where we copy the data directly from the
  610. * user into the GTT, uncached.
  611. */
  612. static int
  613. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  614. struct drm_i915_gem_object *obj,
  615. struct drm_i915_gem_pwrite *args,
  616. struct drm_file *file)
  617. {
  618. struct drm_i915_private *dev_priv = dev->dev_private;
  619. ssize_t remain;
  620. loff_t offset, page_base;
  621. char __user *user_data;
  622. int page_offset, page_length, ret;
  623. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  624. if (ret)
  625. goto out;
  626. ret = i915_gem_object_set_to_gtt_domain(obj, true);
  627. if (ret)
  628. goto out_unpin;
  629. ret = i915_gem_object_put_fence(obj);
  630. if (ret)
  631. goto out_unpin;
  632. user_data = to_user_ptr(args->data_ptr);
  633. remain = args->size;
  634. offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  635. while (remain > 0) {
  636. /* Operation in this page
  637. *
  638. * page_base = page offset within aperture
  639. * page_offset = offset within page
  640. * page_length = bytes to copy for this page
  641. */
  642. page_base = offset & PAGE_MASK;
  643. page_offset = offset_in_page(offset);
  644. page_length = remain;
  645. if ((page_offset + remain) > PAGE_SIZE)
  646. page_length = PAGE_SIZE - page_offset;
  647. /* If we get a fault while copying data, then (presumably) our
  648. * source page isn't available. Return the error and we'll
  649. * retry in the slow path.
  650. */
  651. if (fast_user_write(dev_priv->gtt.mappable, page_base,
  652. page_offset, user_data, page_length)) {
  653. ret = -EFAULT;
  654. goto out_unpin;
  655. }
  656. remain -= page_length;
  657. user_data += page_length;
  658. offset += page_length;
  659. }
  660. out_unpin:
  661. i915_gem_object_ggtt_unpin(obj);
  662. out:
  663. return ret;
  664. }
  665. /* Per-page copy function for the shmem pwrite fastpath.
  666. * Flushes invalid cachelines before writing to the target if
  667. * needs_clflush_before is set and flushes out any written cachelines after
  668. * writing if needs_clflush is set. */
  669. static int
  670. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  671. char __user *user_data,
  672. bool page_do_bit17_swizzling,
  673. bool needs_clflush_before,
  674. bool needs_clflush_after)
  675. {
  676. char *vaddr;
  677. int ret;
  678. if (unlikely(page_do_bit17_swizzling))
  679. return -EINVAL;
  680. vaddr = kmap_atomic(page);
  681. if (needs_clflush_before)
  682. drm_clflush_virt_range(vaddr + shmem_page_offset,
  683. page_length);
  684. ret = __copy_from_user_inatomic(vaddr + shmem_page_offset,
  685. user_data, page_length);
  686. if (needs_clflush_after)
  687. drm_clflush_virt_range(vaddr + shmem_page_offset,
  688. page_length);
  689. kunmap_atomic(vaddr);
  690. return ret ? -EFAULT : 0;
  691. }
  692. /* Only difference to the fast-path function is that this can handle bit17
  693. * and uses non-atomic copy and kmap functions. */
  694. static int
  695. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  696. char __user *user_data,
  697. bool page_do_bit17_swizzling,
  698. bool needs_clflush_before,
  699. bool needs_clflush_after)
  700. {
  701. char *vaddr;
  702. int ret;
  703. vaddr = kmap(page);
  704. if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  705. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  706. page_length,
  707. page_do_bit17_swizzling);
  708. if (page_do_bit17_swizzling)
  709. ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  710. user_data,
  711. page_length);
  712. else
  713. ret = __copy_from_user(vaddr + shmem_page_offset,
  714. user_data,
  715. page_length);
  716. if (needs_clflush_after)
  717. shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  718. page_length,
  719. page_do_bit17_swizzling);
  720. kunmap(page);
  721. return ret ? -EFAULT : 0;
  722. }
  723. static int
  724. i915_gem_shmem_pwrite(struct drm_device *dev,
  725. struct drm_i915_gem_object *obj,
  726. struct drm_i915_gem_pwrite *args,
  727. struct drm_file *file)
  728. {
  729. ssize_t remain;
  730. loff_t offset;
  731. char __user *user_data;
  732. int shmem_page_offset, page_length, ret = 0;
  733. int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  734. int hit_slowpath = 0;
  735. int needs_clflush_after = 0;
  736. int needs_clflush_before = 0;
  737. struct sg_page_iter sg_iter;
  738. user_data = to_user_ptr(args->data_ptr);
  739. remain = args->size;
  740. obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  741. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  742. /* If we're not in the cpu write domain, set ourself into the gtt
  743. * write domain and manually flush cachelines (if required). This
  744. * optimizes for the case when the gpu will use the data
  745. * right away and we therefore have to clflush anyway. */
  746. needs_clflush_after = cpu_write_needs_clflush(obj);
  747. ret = i915_gem_object_wait_rendering(obj, false);
  748. if (ret)
  749. return ret;
  750. i915_gem_object_retire(obj);
  751. }
  752. /* Same trick applies to invalidate partially written cachelines read
  753. * before writing. */
  754. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  755. needs_clflush_before =
  756. !cpu_cache_is_coherent(dev, obj->cache_level);
  757. ret = i915_gem_object_get_pages(obj);
  758. if (ret)
  759. return ret;
  760. i915_gem_object_pin_pages(obj);
  761. offset = args->offset;
  762. obj->dirty = 1;
  763. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  764. offset >> PAGE_SHIFT) {
  765. struct page *page = sg_page_iter_page(&sg_iter);
  766. int partial_cacheline_write;
  767. if (remain <= 0)
  768. break;
  769. /* Operation in this page
  770. *
  771. * shmem_page_offset = offset within page in shmem file
  772. * page_length = bytes to copy for this page
  773. */
  774. shmem_page_offset = offset_in_page(offset);
  775. page_length = remain;
  776. if ((shmem_page_offset + page_length) > PAGE_SIZE)
  777. page_length = PAGE_SIZE - shmem_page_offset;
  778. /* If we don't overwrite a cacheline completely we need to be
  779. * careful to have up-to-date data by first clflushing. Don't
  780. * overcomplicate things and flush the entire patch. */
  781. partial_cacheline_write = needs_clflush_before &&
  782. ((shmem_page_offset | page_length)
  783. & (boot_cpu_data.x86_clflush_size - 1));
  784. page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  785. (page_to_phys(page) & (1 << 17)) != 0;
  786. ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  787. user_data, page_do_bit17_swizzling,
  788. partial_cacheline_write,
  789. needs_clflush_after);
  790. if (ret == 0)
  791. goto next_page;
  792. hit_slowpath = 1;
  793. mutex_unlock(&dev->struct_mutex);
  794. ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  795. user_data, page_do_bit17_swizzling,
  796. partial_cacheline_write,
  797. needs_clflush_after);
  798. mutex_lock(&dev->struct_mutex);
  799. if (ret)
  800. goto out;
  801. next_page:
  802. remain -= page_length;
  803. user_data += page_length;
  804. offset += page_length;
  805. }
  806. out:
  807. i915_gem_object_unpin_pages(obj);
  808. if (hit_slowpath) {
  809. /*
  810. * Fixup: Flush cpu caches in case we didn't flush the dirty
  811. * cachelines in-line while writing and the object moved
  812. * out of the cpu write domain while we've dropped the lock.
  813. */
  814. if (!needs_clflush_after &&
  815. obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  816. if (i915_gem_clflush_object(obj, obj->pin_display))
  817. i915_gem_chipset_flush(dev);
  818. }
  819. }
  820. if (needs_clflush_after)
  821. i915_gem_chipset_flush(dev);
  822. return ret;
  823. }
  824. /**
  825. * Writes data to the object referenced by handle.
  826. *
  827. * On error, the contents of the buffer that were to be modified are undefined.
  828. */
  829. int
  830. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  831. struct drm_file *file)
  832. {
  833. struct drm_i915_gem_pwrite *args = data;
  834. struct drm_i915_gem_object *obj;
  835. int ret;
  836. if (args->size == 0)
  837. return 0;
  838. if (!access_ok(VERIFY_READ,
  839. to_user_ptr(args->data_ptr),
  840. args->size))
  841. return -EFAULT;
  842. if (likely(!i915.prefault_disable)) {
  843. ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
  844. args->size);
  845. if (ret)
  846. return -EFAULT;
  847. }
  848. ret = i915_mutex_lock_interruptible(dev);
  849. if (ret)
  850. return ret;
  851. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  852. if (&obj->base == NULL) {
  853. ret = -ENOENT;
  854. goto unlock;
  855. }
  856. /* Bounds check destination. */
  857. if (args->offset > obj->base.size ||
  858. args->size > obj->base.size - args->offset) {
  859. ret = -EINVAL;
  860. goto out;
  861. }
  862. /* prime objects have no backing filp to GEM pread/pwrite
  863. * pages from.
  864. */
  865. if (!obj->base.filp) {
  866. ret = -EINVAL;
  867. goto out;
  868. }
  869. trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  870. ret = -EFAULT;
  871. /* We can only do the GTT pwrite on untiled buffers, as otherwise
  872. * it would end up going through the fenced access, and we'll get
  873. * different detiling behavior between reading and writing.
  874. * pread/pwrite currently are reading and writing from the CPU
  875. * perspective, requiring manual detiling by the client.
  876. */
  877. if (obj->phys_handle) {
  878. ret = i915_gem_phys_pwrite(obj, args, file);
  879. goto out;
  880. }
  881. if (obj->tiling_mode == I915_TILING_NONE &&
  882. obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  883. cpu_write_needs_clflush(obj)) {
  884. ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  885. /* Note that the gtt paths might fail with non-page-backed user
  886. * pointers (e.g. gtt mappings when moving data between
  887. * textures). Fallback to the shmem path in that case. */
  888. }
  889. if (ret == -EFAULT || ret == -ENOSPC)
  890. ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  891. out:
  892. drm_gem_object_unreference(&obj->base);
  893. unlock:
  894. mutex_unlock(&dev->struct_mutex);
  895. return ret;
  896. }
  897. int
  898. i915_gem_check_wedge(struct i915_gpu_error *error,
  899. bool interruptible)
  900. {
  901. if (i915_reset_in_progress(error)) {
  902. /* Non-interruptible callers can't handle -EAGAIN, hence return
  903. * -EIO unconditionally for these. */
  904. if (!interruptible)
  905. return -EIO;
  906. /* Recovery complete, but the reset failed ... */
  907. if (i915_terminally_wedged(error))
  908. return -EIO;
  909. return -EAGAIN;
  910. }
  911. return 0;
  912. }
  913. /*
  914. * Compare seqno against outstanding lazy request. Emit a request if they are
  915. * equal.
  916. */
  917. static int
  918. i915_gem_check_olr(struct intel_engine_cs *ring, u32 seqno)
  919. {
  920. int ret;
  921. BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  922. ret = 0;
  923. if (seqno == ring->outstanding_lazy_seqno)
  924. ret = i915_add_request(ring, NULL);
  925. return ret;
  926. }
  927. static void fake_irq(unsigned long data)
  928. {
  929. wake_up_process((struct task_struct *)data);
  930. }
  931. static bool missed_irq(struct drm_i915_private *dev_priv,
  932. struct intel_engine_cs *ring)
  933. {
  934. return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  935. }
  936. static bool can_wait_boost(struct drm_i915_file_private *file_priv)
  937. {
  938. if (file_priv == NULL)
  939. return true;
  940. return !atomic_xchg(&file_priv->rps_wait_boost, true);
  941. }
  942. /**
  943. * __wait_seqno - wait until execution of seqno has finished
  944. * @ring: the ring expected to report seqno
  945. * @seqno: duh!
  946. * @reset_counter: reset sequence associated with the given seqno
  947. * @interruptible: do an interruptible wait (normally yes)
  948. * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  949. *
  950. * Note: It is of utmost importance that the passed in seqno and reset_counter
  951. * values have been read by the caller in an smp safe manner. Where read-side
  952. * locks are involved, it is sufficient to read the reset_counter before
  953. * unlocking the lock that protects the seqno. For lockless tricks, the
  954. * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  955. * inserted.
  956. *
  957. * Returns 0 if the seqno was found within the alloted time. Else returns the
  958. * errno with remaining time filled in timeout argument.
  959. */
  960. static int __wait_seqno(struct intel_engine_cs *ring, u32 seqno,
  961. unsigned reset_counter,
  962. bool interruptible,
  963. struct timespec *timeout,
  964. struct drm_i915_file_private *file_priv)
  965. {
  966. struct drm_device *dev = ring->dev;
  967. struct drm_i915_private *dev_priv = dev->dev_private;
  968. const bool irq_test_in_progress =
  969. ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  970. struct timespec before, now;
  971. DEFINE_WAIT(wait);
  972. unsigned long timeout_expire;
  973. int ret;
  974. WARN(dev_priv->pm.irqs_disabled, "IRQs disabled\n");
  975. if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  976. return 0;
  977. timeout_expire = timeout ? jiffies + timespec_to_jiffies_timeout(timeout) : 0;
  978. if (INTEL_INFO(dev)->gen >= 6 && can_wait_boost(file_priv)) {
  979. gen6_rps_boost(dev_priv);
  980. if (file_priv)
  981. mod_delayed_work(dev_priv->wq,
  982. &file_priv->mm.idle_work,
  983. msecs_to_jiffies(100));
  984. }
  985. if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring)))
  986. return -ENODEV;
  987. /* Record current time in case interrupted by signal, or wedged */
  988. trace_i915_gem_request_wait_begin(ring, seqno);
  989. getrawmonotonic(&before);
  990. for (;;) {
  991. struct timer_list timer;
  992. prepare_to_wait(&ring->irq_queue, &wait,
  993. interruptible ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
  994. /* We need to check whether any gpu reset happened in between
  995. * the caller grabbing the seqno and now ... */
  996. if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  997. /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  998. * is truely gone. */
  999. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1000. if (ret == 0)
  1001. ret = -EAGAIN;
  1002. break;
  1003. }
  1004. if (i915_seqno_passed(ring->get_seqno(ring, false), seqno)) {
  1005. ret = 0;
  1006. break;
  1007. }
  1008. if (interruptible && signal_pending(current)) {
  1009. ret = -ERESTARTSYS;
  1010. break;
  1011. }
  1012. if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1013. ret = -ETIME;
  1014. break;
  1015. }
  1016. timer.function = NULL;
  1017. if (timeout || missed_irq(dev_priv, ring)) {
  1018. unsigned long expire;
  1019. setup_timer_on_stack(&timer, fake_irq, (unsigned long)current);
  1020. expire = missed_irq(dev_priv, ring) ? jiffies + 1 : timeout_expire;
  1021. mod_timer(&timer, expire);
  1022. }
  1023. io_schedule();
  1024. if (timer.function) {
  1025. del_singleshot_timer_sync(&timer);
  1026. destroy_timer_on_stack(&timer);
  1027. }
  1028. }
  1029. getrawmonotonic(&now);
  1030. trace_i915_gem_request_wait_end(ring, seqno);
  1031. if (!irq_test_in_progress)
  1032. ring->irq_put(ring);
  1033. finish_wait(&ring->irq_queue, &wait);
  1034. if (timeout) {
  1035. struct timespec sleep_time = timespec_sub(now, before);
  1036. *timeout = timespec_sub(*timeout, sleep_time);
  1037. if (!timespec_valid(timeout)) /* i.e. negative time remains */
  1038. set_normalized_timespec(timeout, 0, 0);
  1039. }
  1040. return ret;
  1041. }
  1042. /**
  1043. * Waits for a sequence number to be signaled, and cleans up the
  1044. * request and object lists appropriately for that event.
  1045. */
  1046. int
  1047. i915_wait_seqno(struct intel_engine_cs *ring, uint32_t seqno)
  1048. {
  1049. struct drm_device *dev = ring->dev;
  1050. struct drm_i915_private *dev_priv = dev->dev_private;
  1051. bool interruptible = dev_priv->mm.interruptible;
  1052. int ret;
  1053. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1054. BUG_ON(seqno == 0);
  1055. ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1056. if (ret)
  1057. return ret;
  1058. ret = i915_gem_check_olr(ring, seqno);
  1059. if (ret)
  1060. return ret;
  1061. return __wait_seqno(ring, seqno,
  1062. atomic_read(&dev_priv->gpu_error.reset_counter),
  1063. interruptible, NULL, NULL);
  1064. }
  1065. static int
  1066. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
  1067. struct intel_engine_cs *ring)
  1068. {
  1069. if (!obj->active)
  1070. return 0;
  1071. /* Manually manage the write flush as we may have not yet
  1072. * retired the buffer.
  1073. *
  1074. * Note that the last_write_seqno is always the earlier of
  1075. * the two (read/write) seqno, so if we haved successfully waited,
  1076. * we know we have passed the last write.
  1077. */
  1078. obj->last_write_seqno = 0;
  1079. return 0;
  1080. }
  1081. /**
  1082. * Ensures that all rendering to the object has completed and the object is
  1083. * safe to unbind from the GTT or access from the CPU.
  1084. */
  1085. static __must_check int
  1086. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1087. bool readonly)
  1088. {
  1089. struct intel_engine_cs *ring = obj->ring;
  1090. u32 seqno;
  1091. int ret;
  1092. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1093. if (seqno == 0)
  1094. return 0;
  1095. ret = i915_wait_seqno(ring, seqno);
  1096. if (ret)
  1097. return ret;
  1098. return i915_gem_object_wait_rendering__tail(obj, ring);
  1099. }
  1100. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1101. * as the object state may change during this call.
  1102. */
  1103. static __must_check int
  1104. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1105. struct drm_i915_file_private *file_priv,
  1106. bool readonly)
  1107. {
  1108. struct drm_device *dev = obj->base.dev;
  1109. struct drm_i915_private *dev_priv = dev->dev_private;
  1110. struct intel_engine_cs *ring = obj->ring;
  1111. unsigned reset_counter;
  1112. u32 seqno;
  1113. int ret;
  1114. BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1115. BUG_ON(!dev_priv->mm.interruptible);
  1116. seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1117. if (seqno == 0)
  1118. return 0;
  1119. ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1120. if (ret)
  1121. return ret;
  1122. ret = i915_gem_check_olr(ring, seqno);
  1123. if (ret)
  1124. return ret;
  1125. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1126. mutex_unlock(&dev->struct_mutex);
  1127. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, file_priv);
  1128. mutex_lock(&dev->struct_mutex);
  1129. if (ret)
  1130. return ret;
  1131. return i915_gem_object_wait_rendering__tail(obj, ring);
  1132. }
  1133. /**
  1134. * Called when user space prepares to use an object with the CPU, either
  1135. * through the mmap ioctl's mapping or a GTT mapping.
  1136. */
  1137. int
  1138. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1139. struct drm_file *file)
  1140. {
  1141. struct drm_i915_gem_set_domain *args = data;
  1142. struct drm_i915_gem_object *obj;
  1143. uint32_t read_domains = args->read_domains;
  1144. uint32_t write_domain = args->write_domain;
  1145. int ret;
  1146. /* Only handle setting domains to types used by the CPU. */
  1147. if (write_domain & I915_GEM_GPU_DOMAINS)
  1148. return -EINVAL;
  1149. if (read_domains & I915_GEM_GPU_DOMAINS)
  1150. return -EINVAL;
  1151. /* Having something in the write domain implies it's in the read
  1152. * domain, and only that read domain. Enforce that in the request.
  1153. */
  1154. if (write_domain != 0 && read_domains != write_domain)
  1155. return -EINVAL;
  1156. ret = i915_mutex_lock_interruptible(dev);
  1157. if (ret)
  1158. return ret;
  1159. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1160. if (&obj->base == NULL) {
  1161. ret = -ENOENT;
  1162. goto unlock;
  1163. }
  1164. /* Try to flush the object off the GPU without holding the lock.
  1165. * We will repeat the flush holding the lock in the normal manner
  1166. * to catch cases where we are gazumped.
  1167. */
  1168. ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1169. file->driver_priv,
  1170. !write_domain);
  1171. if (ret)
  1172. goto unref;
  1173. if (read_domains & I915_GEM_DOMAIN_GTT) {
  1174. ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1175. /* Silently promote "you're not bound, there was nothing to do"
  1176. * to success, since the client was just asking us to
  1177. * make sure everything was done.
  1178. */
  1179. if (ret == -EINVAL)
  1180. ret = 0;
  1181. } else {
  1182. ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1183. }
  1184. unref:
  1185. drm_gem_object_unreference(&obj->base);
  1186. unlock:
  1187. mutex_unlock(&dev->struct_mutex);
  1188. return ret;
  1189. }
  1190. /**
  1191. * Called when user space has done writes to this buffer
  1192. */
  1193. int
  1194. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1195. struct drm_file *file)
  1196. {
  1197. struct drm_i915_gem_sw_finish *args = data;
  1198. struct drm_i915_gem_object *obj;
  1199. int ret = 0;
  1200. ret = i915_mutex_lock_interruptible(dev);
  1201. if (ret)
  1202. return ret;
  1203. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1204. if (&obj->base == NULL) {
  1205. ret = -ENOENT;
  1206. goto unlock;
  1207. }
  1208. /* Pinned buffers may be scanout, so flush the cache */
  1209. if (obj->pin_display)
  1210. i915_gem_object_flush_cpu_write_domain(obj, true);
  1211. drm_gem_object_unreference(&obj->base);
  1212. unlock:
  1213. mutex_unlock(&dev->struct_mutex);
  1214. return ret;
  1215. }
  1216. /**
  1217. * Maps the contents of an object, returning the address it is mapped
  1218. * into.
  1219. *
  1220. * While the mapping holds a reference on the contents of the object, it doesn't
  1221. * imply a ref on the object itself.
  1222. */
  1223. int
  1224. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1225. struct drm_file *file)
  1226. {
  1227. struct drm_i915_gem_mmap *args = data;
  1228. struct drm_gem_object *obj;
  1229. unsigned long addr;
  1230. obj = drm_gem_object_lookup(dev, file, args->handle);
  1231. if (obj == NULL)
  1232. return -ENOENT;
  1233. /* prime objects have no backing filp to GEM mmap
  1234. * pages from.
  1235. */
  1236. if (!obj->filp) {
  1237. drm_gem_object_unreference_unlocked(obj);
  1238. return -EINVAL;
  1239. }
  1240. addr = vm_mmap(obj->filp, 0, args->size,
  1241. PROT_READ | PROT_WRITE, MAP_SHARED,
  1242. args->offset);
  1243. drm_gem_object_unreference_unlocked(obj);
  1244. if (IS_ERR((void *)addr))
  1245. return addr;
  1246. args->addr_ptr = (uint64_t) addr;
  1247. return 0;
  1248. }
  1249. /**
  1250. * i915_gem_fault - fault a page into the GTT
  1251. * vma: VMA in question
  1252. * vmf: fault info
  1253. *
  1254. * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
  1255. * from userspace. The fault handler takes care of binding the object to
  1256. * the GTT (if needed), allocating and programming a fence register (again,
  1257. * only if needed based on whether the old reg is still valid or the object
  1258. * is tiled) and inserting a new PTE into the faulting process.
  1259. *
  1260. * Note that the faulting process may involve evicting existing objects
  1261. * from the GTT and/or fence registers to make room. So performance may
  1262. * suffer if the GTT working set is large or there are few fence registers
  1263. * left.
  1264. */
  1265. int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1266. {
  1267. struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
  1268. struct drm_device *dev = obj->base.dev;
  1269. struct drm_i915_private *dev_priv = dev->dev_private;
  1270. pgoff_t page_offset;
  1271. unsigned long pfn;
  1272. int ret = 0;
  1273. bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
  1274. intel_runtime_pm_get(dev_priv);
  1275. /* We don't use vmf->pgoff since that has the fake offset */
  1276. page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
  1277. PAGE_SHIFT;
  1278. ret = i915_mutex_lock_interruptible(dev);
  1279. if (ret)
  1280. goto out;
  1281. trace_i915_gem_object_fault(obj, page_offset, true, write);
  1282. /* Try to flush the object off the GPU first without holding the lock.
  1283. * Upon reacquiring the lock, we will perform our sanity checks and then
  1284. * repeat the flush holding the lock in the normal manner to catch cases
  1285. * where we are gazumped.
  1286. */
  1287. ret = i915_gem_object_wait_rendering__nonblocking(obj, NULL, !write);
  1288. if (ret)
  1289. goto unlock;
  1290. /* Access to snoopable pages through the GTT is incoherent. */
  1291. if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
  1292. ret = -EFAULT;
  1293. goto unlock;
  1294. }
  1295. /* Now bind it into the GTT if needed */
  1296. ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE);
  1297. if (ret)
  1298. goto unlock;
  1299. ret = i915_gem_object_set_to_gtt_domain(obj, write);
  1300. if (ret)
  1301. goto unpin;
  1302. ret = i915_gem_object_get_fence(obj);
  1303. if (ret)
  1304. goto unpin;
  1305. obj->fault_mappable = true;
  1306. pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1307. pfn >>= PAGE_SHIFT;
  1308. pfn += page_offset;
  1309. /* Finally, remap it using the new GTT offset */
  1310. ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
  1311. unpin:
  1312. i915_gem_object_ggtt_unpin(obj);
  1313. unlock:
  1314. mutex_unlock(&dev->struct_mutex);
  1315. out:
  1316. switch (ret) {
  1317. case -EIO:
  1318. /* If this -EIO is due to a gpu hang, give the reset code a
  1319. * chance to clean up the mess. Otherwise return the proper
  1320. * SIGBUS. */
  1321. if (i915_terminally_wedged(&dev_priv->gpu_error)) {
  1322. ret = VM_FAULT_SIGBUS;
  1323. break;
  1324. }
  1325. case -EAGAIN:
  1326. /*
  1327. * EAGAIN means the gpu is hung and we'll wait for the error
  1328. * handler to reset everything when re-faulting in
  1329. * i915_mutex_lock_interruptible.
  1330. */
  1331. case 0:
  1332. case -ERESTARTSYS:
  1333. case -EINTR:
  1334. case -EBUSY:
  1335. /*
  1336. * EBUSY is ok: this just means that another thread
  1337. * already did the job.
  1338. */
  1339. ret = VM_FAULT_NOPAGE;
  1340. break;
  1341. case -ENOMEM:
  1342. ret = VM_FAULT_OOM;
  1343. break;
  1344. case -ENOSPC:
  1345. case -EFAULT:
  1346. ret = VM_FAULT_SIGBUS;
  1347. break;
  1348. default:
  1349. WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
  1350. ret = VM_FAULT_SIGBUS;
  1351. break;
  1352. }
  1353. intel_runtime_pm_put(dev_priv);
  1354. return ret;
  1355. }
  1356. void i915_gem_release_all_mmaps(struct drm_i915_private *dev_priv)
  1357. {
  1358. struct i915_vma *vma;
  1359. /*
  1360. * Only the global gtt is relevant for gtt memory mappings, so restrict
  1361. * list traversal to objects bound into the global address space. Note
  1362. * that the active list should be empty, but better safe than sorry.
  1363. */
  1364. WARN_ON(!list_empty(&dev_priv->gtt.base.active_list));
  1365. list_for_each_entry(vma, &dev_priv->gtt.base.active_list, mm_list)
  1366. i915_gem_release_mmap(vma->obj);
  1367. list_for_each_entry(vma, &dev_priv->gtt.base.inactive_list, mm_list)
  1368. i915_gem_release_mmap(vma->obj);
  1369. }
  1370. /**
  1371. * i915_gem_release_mmap - remove physical page mappings
  1372. * @obj: obj in question
  1373. *
  1374. * Preserve the reservation of the mmapping with the DRM core code, but
  1375. * relinquish ownership of the pages back to the system.
  1376. *
  1377. * It is vital that we remove the page mapping if we have mapped a tiled
  1378. * object through the GTT and then lose the fence register due to
  1379. * resource pressure. Similarly if the object has been moved out of the
  1380. * aperture, than pages mapped into userspace must be revoked. Removing the
  1381. * mapping will then trigger a page fault on the next user access, allowing
  1382. * fixup by i915_gem_fault().
  1383. */
  1384. void
  1385. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1386. {
  1387. if (!obj->fault_mappable)
  1388. return;
  1389. drm_vma_node_unmap(&obj->base.vma_node,
  1390. obj->base.dev->anon_inode->i_mapping);
  1391. obj->fault_mappable = false;
  1392. }
  1393. uint32_t
  1394. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1395. {
  1396. uint32_t gtt_size;
  1397. if (INTEL_INFO(dev)->gen >= 4 ||
  1398. tiling_mode == I915_TILING_NONE)
  1399. return size;
  1400. /* Previous chips need a power-of-two fence region when tiling */
  1401. if (INTEL_INFO(dev)->gen == 3)
  1402. gtt_size = 1024*1024;
  1403. else
  1404. gtt_size = 512*1024;
  1405. while (gtt_size < size)
  1406. gtt_size <<= 1;
  1407. return gtt_size;
  1408. }
  1409. /**
  1410. * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1411. * @obj: object to check
  1412. *
  1413. * Return the required GTT alignment for an object, taking into account
  1414. * potential fence register mapping.
  1415. */
  1416. uint32_t
  1417. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1418. int tiling_mode, bool fenced)
  1419. {
  1420. /*
  1421. * Minimum alignment is 4k (GTT page size), but might be greater
  1422. * if a fence register is needed for the object.
  1423. */
  1424. if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1425. tiling_mode == I915_TILING_NONE)
  1426. return 4096;
  1427. /*
  1428. * Previous chips need to be aligned to the size of the smallest
  1429. * fence register that can contain the object.
  1430. */
  1431. return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1432. }
  1433. static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
  1434. {
  1435. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1436. int ret;
  1437. if (drm_vma_node_has_offset(&obj->base.vma_node))
  1438. return 0;
  1439. dev_priv->mm.shrinker_no_lock_stealing = true;
  1440. ret = drm_gem_create_mmap_offset(&obj->base);
  1441. if (ret != -ENOSPC)
  1442. goto out;
  1443. /* Badly fragmented mmap space? The only way we can recover
  1444. * space is by destroying unwanted objects. We can't randomly release
  1445. * mmap_offsets as userspace expects them to be persistent for the
  1446. * lifetime of the objects. The closest we can is to release the
  1447. * offsets on purgeable objects by truncating it and marking it purged,
  1448. * which prevents userspace from ever using that object again.
  1449. */
  1450. i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
  1451. ret = drm_gem_create_mmap_offset(&obj->base);
  1452. if (ret != -ENOSPC)
  1453. goto out;
  1454. i915_gem_shrink_all(dev_priv);
  1455. ret = drm_gem_create_mmap_offset(&obj->base);
  1456. out:
  1457. dev_priv->mm.shrinker_no_lock_stealing = false;
  1458. return ret;
  1459. }
  1460. static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
  1461. {
  1462. drm_gem_free_mmap_offset(&obj->base);
  1463. }
  1464. int
  1465. i915_gem_mmap_gtt(struct drm_file *file,
  1466. struct drm_device *dev,
  1467. uint32_t handle,
  1468. uint64_t *offset)
  1469. {
  1470. struct drm_i915_private *dev_priv = dev->dev_private;
  1471. struct drm_i915_gem_object *obj;
  1472. int ret;
  1473. ret = i915_mutex_lock_interruptible(dev);
  1474. if (ret)
  1475. return ret;
  1476. obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1477. if (&obj->base == NULL) {
  1478. ret = -ENOENT;
  1479. goto unlock;
  1480. }
  1481. if (obj->base.size > dev_priv->gtt.mappable_end) {
  1482. ret = -E2BIG;
  1483. goto out;
  1484. }
  1485. if (obj->madv != I915_MADV_WILLNEED) {
  1486. DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1487. ret = -EFAULT;
  1488. goto out;
  1489. }
  1490. ret = i915_gem_object_create_mmap_offset(obj);
  1491. if (ret)
  1492. goto out;
  1493. *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
  1494. out:
  1495. drm_gem_object_unreference(&obj->base);
  1496. unlock:
  1497. mutex_unlock(&dev->struct_mutex);
  1498. return ret;
  1499. }
  1500. /**
  1501. * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1502. * @dev: DRM device
  1503. * @data: GTT mapping ioctl data
  1504. * @file: GEM object info
  1505. *
  1506. * Simply returns the fake offset to userspace so it can mmap it.
  1507. * The mmap call will end up in drm_gem_mmap(), which will set things
  1508. * up so we can get faults in the handler above.
  1509. *
  1510. * The fault handler will take care of binding the object into the GTT
  1511. * (since it may have been evicted to make room for something), allocating
  1512. * a fence register, and mapping the appropriate aperture address into
  1513. * userspace.
  1514. */
  1515. int
  1516. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1517. struct drm_file *file)
  1518. {
  1519. struct drm_i915_gem_mmap_gtt *args = data;
  1520. return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1521. }
  1522. static inline int
  1523. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1524. {
  1525. return obj->madv == I915_MADV_DONTNEED;
  1526. }
  1527. /* Immediately discard the backing storage */
  1528. static void
  1529. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1530. {
  1531. i915_gem_object_free_mmap_offset(obj);
  1532. if (obj->base.filp == NULL)
  1533. return;
  1534. /* Our goal here is to return as much of the memory as
  1535. * is possible back to the system as we are called from OOM.
  1536. * To do this we must instruct the shmfs to drop all of its
  1537. * backing pages, *now*.
  1538. */
  1539. shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1540. obj->madv = __I915_MADV_PURGED;
  1541. }
  1542. /* Try to discard unwanted pages */
  1543. static void
  1544. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1545. {
  1546. struct address_space *mapping;
  1547. switch (obj->madv) {
  1548. case I915_MADV_DONTNEED:
  1549. i915_gem_object_truncate(obj);
  1550. case __I915_MADV_PURGED:
  1551. return;
  1552. }
  1553. if (obj->base.filp == NULL)
  1554. return;
  1555. mapping = file_inode(obj->base.filp)->i_mapping,
  1556. invalidate_mapping_pages(mapping, 0, (loff_t)-1);
  1557. }
  1558. static void
  1559. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1560. {
  1561. struct sg_page_iter sg_iter;
  1562. int ret;
  1563. BUG_ON(obj->madv == __I915_MADV_PURGED);
  1564. ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1565. if (ret) {
  1566. /* In the event of a disaster, abandon all caches and
  1567. * hope for the best.
  1568. */
  1569. WARN_ON(ret != -EIO);
  1570. i915_gem_clflush_object(obj, true);
  1571. obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1572. }
  1573. if (i915_gem_object_needs_bit17_swizzle(obj))
  1574. i915_gem_object_save_bit_17_swizzle(obj);
  1575. if (obj->madv == I915_MADV_DONTNEED)
  1576. obj->dirty = 0;
  1577. for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1578. struct page *page = sg_page_iter_page(&sg_iter);
  1579. if (obj->dirty)
  1580. set_page_dirty(page);
  1581. if (obj->madv == I915_MADV_WILLNEED)
  1582. mark_page_accessed(page);
  1583. page_cache_release(page);
  1584. }
  1585. obj->dirty = 0;
  1586. sg_free_table(obj->pages);
  1587. kfree(obj->pages);
  1588. }
  1589. int
  1590. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1591. {
  1592. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1593. if (obj->pages == NULL)
  1594. return 0;
  1595. if (obj->pages_pin_count)
  1596. return -EBUSY;
  1597. BUG_ON(i915_gem_obj_bound_any(obj));
  1598. /* ->put_pages might need to allocate memory for the bit17 swizzle
  1599. * array, hence protect them from being reaped by removing them from gtt
  1600. * lists early. */
  1601. list_del(&obj->global_list);
  1602. ops->put_pages(obj);
  1603. obj->pages = NULL;
  1604. i915_gem_object_invalidate(obj);
  1605. return 0;
  1606. }
  1607. static unsigned long
  1608. __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
  1609. bool purgeable_only)
  1610. {
  1611. struct list_head still_in_list;
  1612. struct drm_i915_gem_object *obj;
  1613. unsigned long count = 0;
  1614. /*
  1615. * As we may completely rewrite the (un)bound list whilst unbinding
  1616. * (due to retiring requests) we have to strictly process only
  1617. * one element of the list at the time, and recheck the list
  1618. * on every iteration.
  1619. *
  1620. * In particular, we must hold a reference whilst removing the
  1621. * object as we may end up waiting for and/or retiring the objects.
  1622. * This might release the final reference (held by the active list)
  1623. * and result in the object being freed from under us. This is
  1624. * similar to the precautions the eviction code must take whilst
  1625. * removing objects.
  1626. *
  1627. * Also note that although these lists do not hold a reference to
  1628. * the object we can safely grab one here: The final object
  1629. * unreferencing and the bound_list are both protected by the
  1630. * dev->struct_mutex and so we won't ever be able to observe an
  1631. * object on the bound_list with a reference count equals 0.
  1632. */
  1633. INIT_LIST_HEAD(&still_in_list);
  1634. while (count < target && !list_empty(&dev_priv->mm.unbound_list)) {
  1635. obj = list_first_entry(&dev_priv->mm.unbound_list,
  1636. typeof(*obj), global_list);
  1637. list_move_tail(&obj->global_list, &still_in_list);
  1638. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1639. continue;
  1640. drm_gem_object_reference(&obj->base);
  1641. if (i915_gem_object_put_pages(obj) == 0)
  1642. count += obj->base.size >> PAGE_SHIFT;
  1643. drm_gem_object_unreference(&obj->base);
  1644. }
  1645. list_splice(&still_in_list, &dev_priv->mm.unbound_list);
  1646. INIT_LIST_HEAD(&still_in_list);
  1647. while (count < target && !list_empty(&dev_priv->mm.bound_list)) {
  1648. struct i915_vma *vma, *v;
  1649. obj = list_first_entry(&dev_priv->mm.bound_list,
  1650. typeof(*obj), global_list);
  1651. list_move_tail(&obj->global_list, &still_in_list);
  1652. if (!i915_gem_object_is_purgeable(obj) && purgeable_only)
  1653. continue;
  1654. drm_gem_object_reference(&obj->base);
  1655. list_for_each_entry_safe(vma, v, &obj->vma_list, vma_link)
  1656. if (i915_vma_unbind(vma))
  1657. break;
  1658. if (i915_gem_object_put_pages(obj) == 0)
  1659. count += obj->base.size >> PAGE_SHIFT;
  1660. drm_gem_object_unreference(&obj->base);
  1661. }
  1662. list_splice(&still_in_list, &dev_priv->mm.bound_list);
  1663. return count;
  1664. }
  1665. static unsigned long
  1666. i915_gem_purge(struct drm_i915_private *dev_priv, long target)
  1667. {
  1668. return __i915_gem_shrink(dev_priv, target, true);
  1669. }
  1670. static unsigned long
  1671. i915_gem_shrink_all(struct drm_i915_private *dev_priv)
  1672. {
  1673. i915_gem_evict_everything(dev_priv->dev);
  1674. return __i915_gem_shrink(dev_priv, LONG_MAX, false);
  1675. }
  1676. static int
  1677. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1678. {
  1679. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1680. int page_count, i;
  1681. struct address_space *mapping;
  1682. struct sg_table *st;
  1683. struct scatterlist *sg;
  1684. struct sg_page_iter sg_iter;
  1685. struct page *page;
  1686. unsigned long last_pfn = 0; /* suppress gcc warning */
  1687. gfp_t gfp;
  1688. /* Assert that the object is not currently in any GPU domain. As it
  1689. * wasn't in the GTT, there shouldn't be any way it could have been in
  1690. * a GPU cache
  1691. */
  1692. BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1693. BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1694. st = kmalloc(sizeof(*st), GFP_KERNEL);
  1695. if (st == NULL)
  1696. return -ENOMEM;
  1697. page_count = obj->base.size / PAGE_SIZE;
  1698. if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1699. kfree(st);
  1700. return -ENOMEM;
  1701. }
  1702. /* Get the list of pages out of our struct file. They'll be pinned
  1703. * at this point until we release them.
  1704. *
  1705. * Fail silently without starting the shrinker
  1706. */
  1707. mapping = file_inode(obj->base.filp)->i_mapping;
  1708. gfp = mapping_gfp_mask(mapping);
  1709. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1710. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1711. sg = st->sgl;
  1712. st->nents = 0;
  1713. for (i = 0; i < page_count; i++) {
  1714. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1715. if (IS_ERR(page)) {
  1716. i915_gem_purge(dev_priv, page_count);
  1717. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1718. }
  1719. if (IS_ERR(page)) {
  1720. /* We've tried hard to allocate the memory by reaping
  1721. * our own buffer, now let the real VM do its job and
  1722. * go down in flames if truly OOM.
  1723. */
  1724. gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
  1725. gfp |= __GFP_IO | __GFP_WAIT;
  1726. i915_gem_shrink_all(dev_priv);
  1727. page = shmem_read_mapping_page_gfp(mapping, i, gfp);
  1728. if (IS_ERR(page))
  1729. goto err_pages;
  1730. gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
  1731. gfp &= ~(__GFP_IO | __GFP_WAIT);
  1732. }
  1733. #ifdef CONFIG_SWIOTLB
  1734. if (swiotlb_nr_tbl()) {
  1735. st->nents++;
  1736. sg_set_page(sg, page, PAGE_SIZE, 0);
  1737. sg = sg_next(sg);
  1738. continue;
  1739. }
  1740. #endif
  1741. if (!i || page_to_pfn(page) != last_pfn + 1) {
  1742. if (i)
  1743. sg = sg_next(sg);
  1744. st->nents++;
  1745. sg_set_page(sg, page, PAGE_SIZE, 0);
  1746. } else {
  1747. sg->length += PAGE_SIZE;
  1748. }
  1749. last_pfn = page_to_pfn(page);
  1750. /* Check that the i965g/gm workaround works. */
  1751. WARN_ON((gfp & __GFP_DMA32) && (last_pfn >= 0x00100000UL));
  1752. }
  1753. #ifdef CONFIG_SWIOTLB
  1754. if (!swiotlb_nr_tbl())
  1755. #endif
  1756. sg_mark_end(sg);
  1757. obj->pages = st;
  1758. if (i915_gem_object_needs_bit17_swizzle(obj))
  1759. i915_gem_object_do_bit_17_swizzle(obj);
  1760. return 0;
  1761. err_pages:
  1762. sg_mark_end(sg);
  1763. for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1764. page_cache_release(sg_page_iter_page(&sg_iter));
  1765. sg_free_table(st);
  1766. kfree(st);
  1767. /* shmemfs first checks if there is enough memory to allocate the page
  1768. * and reports ENOSPC should there be insufficient, along with the usual
  1769. * ENOMEM for a genuine allocation failure.
  1770. *
  1771. * We use ENOSPC in our driver to mean that we have run out of aperture
  1772. * space and so want to translate the error from shmemfs back to our
  1773. * usual understanding of ENOMEM.
  1774. */
  1775. if (PTR_ERR(page) == -ENOSPC)
  1776. return -ENOMEM;
  1777. else
  1778. return PTR_ERR(page);
  1779. }
  1780. /* Ensure that the associated pages are gathered from the backing storage
  1781. * and pinned into our object. i915_gem_object_get_pages() may be called
  1782. * multiple times before they are released by a single call to
  1783. * i915_gem_object_put_pages() - once the pages are no longer referenced
  1784. * either as a result of memory pressure (reaping pages under the shrinker)
  1785. * or as the object is itself released.
  1786. */
  1787. int
  1788. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1789. {
  1790. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1791. const struct drm_i915_gem_object_ops *ops = obj->ops;
  1792. int ret;
  1793. if (obj->pages)
  1794. return 0;
  1795. if (obj->madv != I915_MADV_WILLNEED) {
  1796. DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1797. return -EFAULT;
  1798. }
  1799. BUG_ON(obj->pages_pin_count);
  1800. ret = ops->get_pages(obj);
  1801. if (ret)
  1802. return ret;
  1803. list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1804. return 0;
  1805. }
  1806. static void
  1807. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1808. struct intel_engine_cs *ring)
  1809. {
  1810. struct drm_device *dev = obj->base.dev;
  1811. struct drm_i915_private *dev_priv = dev->dev_private;
  1812. u32 seqno = intel_ring_get_seqno(ring);
  1813. BUG_ON(ring == NULL);
  1814. if (obj->ring != ring && obj->last_write_seqno) {
  1815. /* Keep the seqno relative to the current ring */
  1816. obj->last_write_seqno = seqno;
  1817. }
  1818. obj->ring = ring;
  1819. /* Add a reference if we're newly entering the active list. */
  1820. if (!obj->active) {
  1821. drm_gem_object_reference(&obj->base);
  1822. obj->active = 1;
  1823. }
  1824. list_move_tail(&obj->ring_list, &ring->active_list);
  1825. obj->last_read_seqno = seqno;
  1826. if (obj->fenced_gpu_access) {
  1827. obj->last_fenced_seqno = seqno;
  1828. /* Bump MRU to take account of the delayed flush */
  1829. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  1830. struct drm_i915_fence_reg *reg;
  1831. reg = &dev_priv->fence_regs[obj->fence_reg];
  1832. list_move_tail(&reg->lru_list,
  1833. &dev_priv->mm.fence_list);
  1834. }
  1835. }
  1836. }
  1837. void i915_vma_move_to_active(struct i915_vma *vma,
  1838. struct intel_engine_cs *ring)
  1839. {
  1840. list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1841. return i915_gem_object_move_to_active(vma->obj, ring);
  1842. }
  1843. static void
  1844. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1845. {
  1846. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1847. struct i915_address_space *vm;
  1848. struct i915_vma *vma;
  1849. BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1850. BUG_ON(!obj->active);
  1851. list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1852. vma = i915_gem_obj_to_vma(obj, vm);
  1853. if (vma && !list_empty(&vma->mm_list))
  1854. list_move_tail(&vma->mm_list, &vm->inactive_list);
  1855. }
  1856. list_del_init(&obj->ring_list);
  1857. obj->ring = NULL;
  1858. obj->last_read_seqno = 0;
  1859. obj->last_write_seqno = 0;
  1860. obj->base.write_domain = 0;
  1861. obj->last_fenced_seqno = 0;
  1862. obj->fenced_gpu_access = false;
  1863. obj->active = 0;
  1864. drm_gem_object_unreference(&obj->base);
  1865. WARN_ON(i915_verify_lists(dev));
  1866. }
  1867. static void
  1868. i915_gem_object_retire(struct drm_i915_gem_object *obj)
  1869. {
  1870. struct intel_engine_cs *ring = obj->ring;
  1871. if (ring == NULL)
  1872. return;
  1873. if (i915_seqno_passed(ring->get_seqno(ring, true),
  1874. obj->last_read_seqno))
  1875. i915_gem_object_move_to_inactive(obj);
  1876. }
  1877. static int
  1878. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1879. {
  1880. struct drm_i915_private *dev_priv = dev->dev_private;
  1881. struct intel_engine_cs *ring;
  1882. int ret, i, j;
  1883. /* Carefully retire all requests without writing to the rings */
  1884. for_each_ring(ring, dev_priv, i) {
  1885. ret = intel_ring_idle(ring);
  1886. if (ret)
  1887. return ret;
  1888. }
  1889. i915_gem_retire_requests(dev);
  1890. /* Finally reset hw state */
  1891. for_each_ring(ring, dev_priv, i) {
  1892. intel_ring_init_seqno(ring, seqno);
  1893. for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  1894. ring->semaphore.sync_seqno[j] = 0;
  1895. }
  1896. return 0;
  1897. }
  1898. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1899. {
  1900. struct drm_i915_private *dev_priv = dev->dev_private;
  1901. int ret;
  1902. if (seqno == 0)
  1903. return -EINVAL;
  1904. /* HWS page needs to be set less than what we
  1905. * will inject to ring
  1906. */
  1907. ret = i915_gem_init_seqno(dev, seqno - 1);
  1908. if (ret)
  1909. return ret;
  1910. /* Carefully set the last_seqno value so that wrap
  1911. * detection still works
  1912. */
  1913. dev_priv->next_seqno = seqno;
  1914. dev_priv->last_seqno = seqno - 1;
  1915. if (dev_priv->last_seqno == 0)
  1916. dev_priv->last_seqno--;
  1917. return 0;
  1918. }
  1919. int
  1920. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1921. {
  1922. struct drm_i915_private *dev_priv = dev->dev_private;
  1923. /* reserve 0 for non-seqno */
  1924. if (dev_priv->next_seqno == 0) {
  1925. int ret = i915_gem_init_seqno(dev, 0);
  1926. if (ret)
  1927. return ret;
  1928. dev_priv->next_seqno = 1;
  1929. }
  1930. *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1931. return 0;
  1932. }
  1933. int __i915_add_request(struct intel_engine_cs *ring,
  1934. struct drm_file *file,
  1935. struct drm_i915_gem_object *obj,
  1936. u32 *out_seqno)
  1937. {
  1938. struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1939. struct drm_i915_gem_request *request;
  1940. u32 request_ring_position, request_start;
  1941. int ret;
  1942. request_start = intel_ring_get_tail(ring);
  1943. /*
  1944. * Emit any outstanding flushes - execbuf can fail to emit the flush
  1945. * after having emitted the batchbuffer command. Hence we need to fix
  1946. * things up similar to emitting the lazy request. The difference here
  1947. * is that the flush _must_ happen before the next request, no matter
  1948. * what.
  1949. */
  1950. ret = intel_ring_flush_all_caches(ring);
  1951. if (ret)
  1952. return ret;
  1953. request = ring->preallocated_lazy_request;
  1954. if (WARN_ON(request == NULL))
  1955. return -ENOMEM;
  1956. /* Record the position of the start of the request so that
  1957. * should we detect the updated seqno part-way through the
  1958. * GPU processing the request, we never over-estimate the
  1959. * position of the head.
  1960. */
  1961. request_ring_position = intel_ring_get_tail(ring);
  1962. ret = ring->add_request(ring);
  1963. if (ret)
  1964. return ret;
  1965. request->seqno = intel_ring_get_seqno(ring);
  1966. request->ring = ring;
  1967. request->head = request_start;
  1968. request->tail = request_ring_position;
  1969. /* Whilst this request exists, batch_obj will be on the
  1970. * active_list, and so will hold the active reference. Only when this
  1971. * request is retired will the the batch_obj be moved onto the
  1972. * inactive_list and lose its active reference. Hence we do not need
  1973. * to explicitly hold another reference here.
  1974. */
  1975. request->batch_obj = obj;
  1976. /* Hold a reference to the current context so that we can inspect
  1977. * it later in case a hangcheck error event fires.
  1978. */
  1979. request->ctx = ring->last_context;
  1980. if (request->ctx)
  1981. i915_gem_context_reference(request->ctx);
  1982. request->emitted_jiffies = jiffies;
  1983. list_add_tail(&request->list, &ring->request_list);
  1984. request->file_priv = NULL;
  1985. if (file) {
  1986. struct drm_i915_file_private *file_priv = file->driver_priv;
  1987. spin_lock(&file_priv->mm.lock);
  1988. request->file_priv = file_priv;
  1989. list_add_tail(&request->client_list,
  1990. &file_priv->mm.request_list);
  1991. spin_unlock(&file_priv->mm.lock);
  1992. }
  1993. trace_i915_gem_request_add(ring, request->seqno);
  1994. ring->outstanding_lazy_seqno = 0;
  1995. ring->preallocated_lazy_request = NULL;
  1996. if (!dev_priv->ums.mm_suspended) {
  1997. i915_queue_hangcheck(ring->dev);
  1998. cancel_delayed_work_sync(&dev_priv->mm.idle_work);
  1999. queue_delayed_work(dev_priv->wq,
  2000. &dev_priv->mm.retire_work,
  2001. round_jiffies_up_relative(HZ));
  2002. intel_mark_busy(dev_priv->dev);
  2003. }
  2004. if (out_seqno)
  2005. *out_seqno = request->seqno;
  2006. return 0;
  2007. }
  2008. static inline void
  2009. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  2010. {
  2011. struct drm_i915_file_private *file_priv = request->file_priv;
  2012. if (!file_priv)
  2013. return;
  2014. spin_lock(&file_priv->mm.lock);
  2015. list_del(&request->client_list);
  2016. request->file_priv = NULL;
  2017. spin_unlock(&file_priv->mm.lock);
  2018. }
  2019. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2020. const struct intel_context *ctx)
  2021. {
  2022. unsigned long elapsed;
  2023. elapsed = get_seconds() - ctx->hang_stats.guilty_ts;
  2024. if (ctx->hang_stats.banned)
  2025. return true;
  2026. if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
  2027. if (!i915_gem_context_is_default(ctx)) {
  2028. DRM_DEBUG("context hanging too fast, banning!\n");
  2029. return true;
  2030. } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2031. if (i915_stop_ring_allow_warn(dev_priv))
  2032. DRM_ERROR("gpu hanging too fast, banning!\n");
  2033. return true;
  2034. }
  2035. }
  2036. return false;
  2037. }
  2038. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2039. struct intel_context *ctx,
  2040. const bool guilty)
  2041. {
  2042. struct i915_ctx_hang_stats *hs;
  2043. if (WARN_ON(!ctx))
  2044. return;
  2045. hs = &ctx->hang_stats;
  2046. if (guilty) {
  2047. hs->banned = i915_context_is_banned(dev_priv, ctx);
  2048. hs->batch_active++;
  2049. hs->guilty_ts = get_seconds();
  2050. } else {
  2051. hs->batch_pending++;
  2052. }
  2053. }
  2054. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  2055. {
  2056. list_del(&request->list);
  2057. i915_gem_request_remove_from_client(request);
  2058. if (request->ctx)
  2059. i915_gem_context_unreference(request->ctx);
  2060. kfree(request);
  2061. }
  2062. struct drm_i915_gem_request *
  2063. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2064. {
  2065. struct drm_i915_gem_request *request;
  2066. u32 completed_seqno;
  2067. completed_seqno = ring->get_seqno(ring, false);
  2068. list_for_each_entry(request, &ring->request_list, list) {
  2069. if (i915_seqno_passed(completed_seqno, request->seqno))
  2070. continue;
  2071. return request;
  2072. }
  2073. return NULL;
  2074. }
  2075. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2076. struct intel_engine_cs *ring)
  2077. {
  2078. struct drm_i915_gem_request *request;
  2079. bool ring_hung;
  2080. request = i915_gem_find_active_request(ring);
  2081. if (request == NULL)
  2082. return;
  2083. ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2084. i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2085. list_for_each_entry_continue(request, &ring->request_list, list)
  2086. i915_set_reset_status(dev_priv, request->ctx, false);
  2087. }
  2088. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2089. struct intel_engine_cs *ring)
  2090. {
  2091. while (!list_empty(&ring->active_list)) {
  2092. struct drm_i915_gem_object *obj;
  2093. obj = list_first_entry(&ring->active_list,
  2094. struct drm_i915_gem_object,
  2095. ring_list);
  2096. i915_gem_object_move_to_inactive(obj);
  2097. }
  2098. /*
  2099. * We must free the requests after all the corresponding objects have
  2100. * been moved off active lists. Which is the same order as the normal
  2101. * retire_requests function does. This is important if object hold
  2102. * implicit references on things like e.g. ppgtt address spaces through
  2103. * the request.
  2104. */
  2105. while (!list_empty(&ring->request_list)) {
  2106. struct drm_i915_gem_request *request;
  2107. request = list_first_entry(&ring->request_list,
  2108. struct drm_i915_gem_request,
  2109. list);
  2110. i915_gem_free_request(request);
  2111. }
  2112. /* These may not have been flush before the reset, do so now */
  2113. kfree(ring->preallocated_lazy_request);
  2114. ring->preallocated_lazy_request = NULL;
  2115. ring->outstanding_lazy_seqno = 0;
  2116. }
  2117. void i915_gem_restore_fences(struct drm_device *dev)
  2118. {
  2119. struct drm_i915_private *dev_priv = dev->dev_private;
  2120. int i;
  2121. for (i = 0; i < dev_priv->num_fence_regs; i++) {
  2122. struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  2123. /*
  2124. * Commit delayed tiling changes if we have an object still
  2125. * attached to the fence, otherwise just clear the fence.
  2126. */
  2127. if (reg->obj) {
  2128. i915_gem_object_update_fence(reg->obj, reg,
  2129. reg->obj->tiling_mode);
  2130. } else {
  2131. i915_gem_write_fence(dev, i, NULL);
  2132. }
  2133. }
  2134. }
  2135. void i915_gem_reset(struct drm_device *dev)
  2136. {
  2137. struct drm_i915_private *dev_priv = dev->dev_private;
  2138. struct intel_engine_cs *ring;
  2139. int i;
  2140. /*
  2141. * Before we free the objects from the requests, we need to inspect
  2142. * them for finding the guilty party. As the requests only borrow
  2143. * their reference to the objects, the inspection must be done first.
  2144. */
  2145. for_each_ring(ring, dev_priv, i)
  2146. i915_gem_reset_ring_status(dev_priv, ring);
  2147. for_each_ring(ring, dev_priv, i)
  2148. i915_gem_reset_ring_cleanup(dev_priv, ring);
  2149. i915_gem_context_reset(dev);
  2150. i915_gem_restore_fences(dev);
  2151. }
  2152. /**
  2153. * This function clears the request list as sequence numbers are passed.
  2154. */
  2155. void
  2156. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2157. {
  2158. uint32_t seqno;
  2159. if (list_empty(&ring->request_list))
  2160. return;
  2161. WARN_ON(i915_verify_lists(ring->dev));
  2162. seqno = ring->get_seqno(ring, true);
  2163. /* Move any buffers on the active list that are no longer referenced
  2164. * by the ringbuffer to the flushing/inactive lists as appropriate,
  2165. * before we free the context associated with the requests.
  2166. */
  2167. while (!list_empty(&ring->active_list)) {
  2168. struct drm_i915_gem_object *obj;
  2169. obj = list_first_entry(&ring->active_list,
  2170. struct drm_i915_gem_object,
  2171. ring_list);
  2172. if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  2173. break;
  2174. i915_gem_object_move_to_inactive(obj);
  2175. }
  2176. while (!list_empty(&ring->request_list)) {
  2177. struct drm_i915_gem_request *request;
  2178. request = list_first_entry(&ring->request_list,
  2179. struct drm_i915_gem_request,
  2180. list);
  2181. if (!i915_seqno_passed(seqno, request->seqno))
  2182. break;
  2183. trace_i915_gem_request_retire(ring, request->seqno);
  2184. /* We know the GPU must have read the request to have
  2185. * sent us the seqno + interrupt, so use the position
  2186. * of tail of the request to update the last known position
  2187. * of the GPU head.
  2188. */
  2189. ring->buffer->last_retired_head = request->tail;
  2190. i915_gem_free_request(request);
  2191. }
  2192. if (unlikely(ring->trace_irq_seqno &&
  2193. i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  2194. ring->irq_put(ring);
  2195. ring->trace_irq_seqno = 0;
  2196. }
  2197. WARN_ON(i915_verify_lists(ring->dev));
  2198. }
  2199. bool
  2200. i915_gem_retire_requests(struct drm_device *dev)
  2201. {
  2202. struct drm_i915_private *dev_priv = dev->dev_private;
  2203. struct intel_engine_cs *ring;
  2204. bool idle = true;
  2205. int i;
  2206. for_each_ring(ring, dev_priv, i) {
  2207. i915_gem_retire_requests_ring(ring);
  2208. idle &= list_empty(&ring->request_list);
  2209. }
  2210. if (idle)
  2211. mod_delayed_work(dev_priv->wq,
  2212. &dev_priv->mm.idle_work,
  2213. msecs_to_jiffies(100));
  2214. return idle;
  2215. }
  2216. static void
  2217. i915_gem_retire_work_handler(struct work_struct *work)
  2218. {
  2219. struct drm_i915_private *dev_priv =
  2220. container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2221. struct drm_device *dev = dev_priv->dev;
  2222. bool idle;
  2223. /* Come back later if the device is busy... */
  2224. idle = false;
  2225. if (mutex_trylock(&dev->struct_mutex)) {
  2226. idle = i915_gem_retire_requests(dev);
  2227. mutex_unlock(&dev->struct_mutex);
  2228. }
  2229. if (!idle)
  2230. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2231. round_jiffies_up_relative(HZ));
  2232. }
  2233. static void
  2234. i915_gem_idle_work_handler(struct work_struct *work)
  2235. {
  2236. struct drm_i915_private *dev_priv =
  2237. container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2238. intel_mark_idle(dev_priv->dev);
  2239. }
  2240. /**
  2241. * Ensures that an object will eventually get non-busy by flushing any required
  2242. * write domains, emitting any outstanding lazy request and retiring and
  2243. * completed requests.
  2244. */
  2245. static int
  2246. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2247. {
  2248. int ret;
  2249. if (obj->active) {
  2250. ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2251. if (ret)
  2252. return ret;
  2253. i915_gem_retire_requests_ring(obj->ring);
  2254. }
  2255. return 0;
  2256. }
  2257. /**
  2258. * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2259. * @DRM_IOCTL_ARGS: standard ioctl arguments
  2260. *
  2261. * Returns 0 if successful, else an error is returned with the remaining time in
  2262. * the timeout parameter.
  2263. * -ETIME: object is still busy after timeout
  2264. * -ERESTARTSYS: signal interrupted the wait
  2265. * -ENONENT: object doesn't exist
  2266. * Also possible, but rare:
  2267. * -EAGAIN: GPU wedged
  2268. * -ENOMEM: damn
  2269. * -ENODEV: Internal IRQ fail
  2270. * -E?: The add request failed
  2271. *
  2272. * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2273. * non-zero timeout parameter the wait ioctl will wait for the given number of
  2274. * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2275. * without holding struct_mutex the object may become re-busied before this
  2276. * function completes. A similar but shorter * race condition exists in the busy
  2277. * ioctl
  2278. */
  2279. int
  2280. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2281. {
  2282. struct drm_i915_private *dev_priv = dev->dev_private;
  2283. struct drm_i915_gem_wait *args = data;
  2284. struct drm_i915_gem_object *obj;
  2285. struct intel_engine_cs *ring = NULL;
  2286. struct timespec timeout_stack, *timeout = NULL;
  2287. unsigned reset_counter;
  2288. u32 seqno = 0;
  2289. int ret = 0;
  2290. if (args->timeout_ns >= 0) {
  2291. timeout_stack = ns_to_timespec(args->timeout_ns);
  2292. timeout = &timeout_stack;
  2293. }
  2294. ret = i915_mutex_lock_interruptible(dev);
  2295. if (ret)
  2296. return ret;
  2297. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2298. if (&obj->base == NULL) {
  2299. mutex_unlock(&dev->struct_mutex);
  2300. return -ENOENT;
  2301. }
  2302. /* Need to make sure the object gets inactive eventually. */
  2303. ret = i915_gem_object_flush_active(obj);
  2304. if (ret)
  2305. goto out;
  2306. if (obj->active) {
  2307. seqno = obj->last_read_seqno;
  2308. ring = obj->ring;
  2309. }
  2310. if (seqno == 0)
  2311. goto out;
  2312. /* Do this after OLR check to make sure we make forward progress polling
  2313. * on this IOCTL with a 0 timeout (like busy ioctl)
  2314. */
  2315. if (!args->timeout_ns) {
  2316. ret = -ETIME;
  2317. goto out;
  2318. }
  2319. drm_gem_object_unreference(&obj->base);
  2320. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2321. mutex_unlock(&dev->struct_mutex);
  2322. ret = __wait_seqno(ring, seqno, reset_counter, true, timeout, file->driver_priv);
  2323. if (timeout)
  2324. args->timeout_ns = timespec_to_ns(timeout);
  2325. return ret;
  2326. out:
  2327. drm_gem_object_unreference(&obj->base);
  2328. mutex_unlock(&dev->struct_mutex);
  2329. return ret;
  2330. }
  2331. /**
  2332. * i915_gem_object_sync - sync an object to a ring.
  2333. *
  2334. * @obj: object which may be in use on another ring.
  2335. * @to: ring we wish to use the object on. May be NULL.
  2336. *
  2337. * This code is meant to abstract object synchronization with the GPU.
  2338. * Calling with NULL implies synchronizing the object with the CPU
  2339. * rather than a particular GPU ring.
  2340. *
  2341. * Returns 0 if successful, else propagates up the lower layer error.
  2342. */
  2343. int
  2344. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2345. struct intel_engine_cs *to)
  2346. {
  2347. struct intel_engine_cs *from = obj->ring;
  2348. u32 seqno;
  2349. int ret, idx;
  2350. if (from == NULL || to == from)
  2351. return 0;
  2352. if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2353. return i915_gem_object_wait_rendering(obj, false);
  2354. idx = intel_ring_sync_index(from, to);
  2355. seqno = obj->last_read_seqno;
  2356. if (seqno <= from->semaphore.sync_seqno[idx])
  2357. return 0;
  2358. ret = i915_gem_check_olr(obj->ring, seqno);
  2359. if (ret)
  2360. return ret;
  2361. trace_i915_gem_ring_sync_to(from, to, seqno);
  2362. ret = to->semaphore.sync_to(to, from, seqno);
  2363. if (!ret)
  2364. /* We use last_read_seqno because sync_to()
  2365. * might have just caused seqno wrap under
  2366. * the radar.
  2367. */
  2368. from->semaphore.sync_seqno[idx] = obj->last_read_seqno;
  2369. return ret;
  2370. }
  2371. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2372. {
  2373. u32 old_write_domain, old_read_domains;
  2374. /* Force a pagefault for domain tracking on next user access */
  2375. i915_gem_release_mmap(obj);
  2376. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2377. return;
  2378. /* Wait for any direct GTT access to complete */
  2379. mb();
  2380. old_read_domains = obj->base.read_domains;
  2381. old_write_domain = obj->base.write_domain;
  2382. obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2383. obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2384. trace_i915_gem_object_change_domain(obj,
  2385. old_read_domains,
  2386. old_write_domain);
  2387. }
  2388. int i915_vma_unbind(struct i915_vma *vma)
  2389. {
  2390. struct drm_i915_gem_object *obj = vma->obj;
  2391. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2392. int ret;
  2393. if (list_empty(&vma->vma_link))
  2394. return 0;
  2395. if (!drm_mm_node_allocated(&vma->node)) {
  2396. i915_gem_vma_destroy(vma);
  2397. return 0;
  2398. }
  2399. if (vma->pin_count)
  2400. return -EBUSY;
  2401. BUG_ON(obj->pages == NULL);
  2402. ret = i915_gem_object_finish_gpu(obj);
  2403. if (ret)
  2404. return ret;
  2405. /* Continue on if we fail due to EIO, the GPU is hung so we
  2406. * should be safe and we need to cleanup or else we might
  2407. * cause memory corruption through use-after-free.
  2408. */
  2409. if (i915_is_ggtt(vma->vm)) {
  2410. i915_gem_object_finish_gtt(obj);
  2411. /* release the fence reg _after_ flushing */
  2412. ret = i915_gem_object_put_fence(obj);
  2413. if (ret)
  2414. return ret;
  2415. }
  2416. trace_i915_vma_unbind(vma);
  2417. vma->unbind_vma(vma);
  2418. i915_gem_gtt_finish_object(obj);
  2419. list_del_init(&vma->mm_list);
  2420. /* Avoid an unnecessary call to unbind on rebind. */
  2421. if (i915_is_ggtt(vma->vm))
  2422. obj->map_and_fenceable = true;
  2423. drm_mm_remove_node(&vma->node);
  2424. i915_gem_vma_destroy(vma);
  2425. /* Since the unbound list is global, only move to that list if
  2426. * no more VMAs exist. */
  2427. if (list_empty(&obj->vma_list))
  2428. list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2429. /* And finally now the object is completely decoupled from this vma,
  2430. * we can drop its hold on the backing storage and allow it to be
  2431. * reaped by the shrinker.
  2432. */
  2433. i915_gem_object_unpin_pages(obj);
  2434. return 0;
  2435. }
  2436. int i915_gpu_idle(struct drm_device *dev)
  2437. {
  2438. struct drm_i915_private *dev_priv = dev->dev_private;
  2439. struct intel_engine_cs *ring;
  2440. int ret, i;
  2441. /* Flush everything onto the inactive list. */
  2442. for_each_ring(ring, dev_priv, i) {
  2443. ret = i915_switch_context(ring, ring->default_context);
  2444. if (ret)
  2445. return ret;
  2446. ret = intel_ring_idle(ring);
  2447. if (ret)
  2448. return ret;
  2449. }
  2450. return 0;
  2451. }
  2452. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2453. struct drm_i915_gem_object *obj)
  2454. {
  2455. struct drm_i915_private *dev_priv = dev->dev_private;
  2456. int fence_reg;
  2457. int fence_pitch_shift;
  2458. if (INTEL_INFO(dev)->gen >= 6) {
  2459. fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2460. fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2461. } else {
  2462. fence_reg = FENCE_REG_965_0;
  2463. fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2464. }
  2465. fence_reg += reg * 8;
  2466. /* To w/a incoherency with non-atomic 64-bit register updates,
  2467. * we split the 64-bit update into two 32-bit writes. In order
  2468. * for a partial fence not to be evaluated between writes, we
  2469. * precede the update with write to turn off the fence register,
  2470. * and only enable the fence as the last step.
  2471. *
  2472. * For extra levels of paranoia, we make sure each step lands
  2473. * before applying the next step.
  2474. */
  2475. I915_WRITE(fence_reg, 0);
  2476. POSTING_READ(fence_reg);
  2477. if (obj) {
  2478. u32 size = i915_gem_obj_ggtt_size(obj);
  2479. uint64_t val;
  2480. val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2481. 0xfffff000) << 32;
  2482. val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2483. val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2484. if (obj->tiling_mode == I915_TILING_Y)
  2485. val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2486. val |= I965_FENCE_REG_VALID;
  2487. I915_WRITE(fence_reg + 4, val >> 32);
  2488. POSTING_READ(fence_reg + 4);
  2489. I915_WRITE(fence_reg + 0, val);
  2490. POSTING_READ(fence_reg);
  2491. } else {
  2492. I915_WRITE(fence_reg + 4, 0);
  2493. POSTING_READ(fence_reg + 4);
  2494. }
  2495. }
  2496. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2497. struct drm_i915_gem_object *obj)
  2498. {
  2499. struct drm_i915_private *dev_priv = dev->dev_private;
  2500. u32 val;
  2501. if (obj) {
  2502. u32 size = i915_gem_obj_ggtt_size(obj);
  2503. int pitch_val;
  2504. int tile_width;
  2505. WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2506. (size & -size) != size ||
  2507. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2508. "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2509. i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2510. if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2511. tile_width = 128;
  2512. else
  2513. tile_width = 512;
  2514. /* Note: pitch better be a power of two tile widths */
  2515. pitch_val = obj->stride / tile_width;
  2516. pitch_val = ffs(pitch_val) - 1;
  2517. val = i915_gem_obj_ggtt_offset(obj);
  2518. if (obj->tiling_mode == I915_TILING_Y)
  2519. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2520. val |= I915_FENCE_SIZE_BITS(size);
  2521. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2522. val |= I830_FENCE_REG_VALID;
  2523. } else
  2524. val = 0;
  2525. if (reg < 8)
  2526. reg = FENCE_REG_830_0 + reg * 4;
  2527. else
  2528. reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2529. I915_WRITE(reg, val);
  2530. POSTING_READ(reg);
  2531. }
  2532. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2533. struct drm_i915_gem_object *obj)
  2534. {
  2535. struct drm_i915_private *dev_priv = dev->dev_private;
  2536. uint32_t val;
  2537. if (obj) {
  2538. u32 size = i915_gem_obj_ggtt_size(obj);
  2539. uint32_t pitch_val;
  2540. WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2541. (size & -size) != size ||
  2542. (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2543. "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2544. i915_gem_obj_ggtt_offset(obj), size);
  2545. pitch_val = obj->stride / 128;
  2546. pitch_val = ffs(pitch_val) - 1;
  2547. val = i915_gem_obj_ggtt_offset(obj);
  2548. if (obj->tiling_mode == I915_TILING_Y)
  2549. val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2550. val |= I830_FENCE_SIZE_BITS(size);
  2551. val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2552. val |= I830_FENCE_REG_VALID;
  2553. } else
  2554. val = 0;
  2555. I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2556. POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2557. }
  2558. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2559. {
  2560. return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2561. }
  2562. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2563. struct drm_i915_gem_object *obj)
  2564. {
  2565. struct drm_i915_private *dev_priv = dev->dev_private;
  2566. /* Ensure that all CPU reads are completed before installing a fence
  2567. * and all writes before removing the fence.
  2568. */
  2569. if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2570. mb();
  2571. WARN(obj && (!obj->stride || !obj->tiling_mode),
  2572. "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2573. obj->stride, obj->tiling_mode);
  2574. switch (INTEL_INFO(dev)->gen) {
  2575. case 8:
  2576. case 7:
  2577. case 6:
  2578. case 5:
  2579. case 4: i965_write_fence_reg(dev, reg, obj); break;
  2580. case 3: i915_write_fence_reg(dev, reg, obj); break;
  2581. case 2: i830_write_fence_reg(dev, reg, obj); break;
  2582. default: BUG();
  2583. }
  2584. /* And similarly be paranoid that no direct access to this region
  2585. * is reordered to before the fence is installed.
  2586. */
  2587. if (i915_gem_object_needs_mb(obj))
  2588. mb();
  2589. }
  2590. static inline int fence_number(struct drm_i915_private *dev_priv,
  2591. struct drm_i915_fence_reg *fence)
  2592. {
  2593. return fence - dev_priv->fence_regs;
  2594. }
  2595. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2596. struct drm_i915_fence_reg *fence,
  2597. bool enable)
  2598. {
  2599. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2600. int reg = fence_number(dev_priv, fence);
  2601. i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2602. if (enable) {
  2603. obj->fence_reg = reg;
  2604. fence->obj = obj;
  2605. list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2606. } else {
  2607. obj->fence_reg = I915_FENCE_REG_NONE;
  2608. fence->obj = NULL;
  2609. list_del_init(&fence->lru_list);
  2610. }
  2611. obj->fence_dirty = false;
  2612. }
  2613. static int
  2614. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2615. {
  2616. if (obj->last_fenced_seqno) {
  2617. int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2618. if (ret)
  2619. return ret;
  2620. obj->last_fenced_seqno = 0;
  2621. }
  2622. obj->fenced_gpu_access = false;
  2623. return 0;
  2624. }
  2625. int
  2626. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2627. {
  2628. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2629. struct drm_i915_fence_reg *fence;
  2630. int ret;
  2631. ret = i915_gem_object_wait_fence(obj);
  2632. if (ret)
  2633. return ret;
  2634. if (obj->fence_reg == I915_FENCE_REG_NONE)
  2635. return 0;
  2636. fence = &dev_priv->fence_regs[obj->fence_reg];
  2637. if (WARN_ON(fence->pin_count))
  2638. return -EBUSY;
  2639. i915_gem_object_fence_lost(obj);
  2640. i915_gem_object_update_fence(obj, fence, false);
  2641. return 0;
  2642. }
  2643. static struct drm_i915_fence_reg *
  2644. i915_find_fence_reg(struct drm_device *dev)
  2645. {
  2646. struct drm_i915_private *dev_priv = dev->dev_private;
  2647. struct drm_i915_fence_reg *reg, *avail;
  2648. int i;
  2649. /* First try to find a free reg */
  2650. avail = NULL;
  2651. for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2652. reg = &dev_priv->fence_regs[i];
  2653. if (!reg->obj)
  2654. return reg;
  2655. if (!reg->pin_count)
  2656. avail = reg;
  2657. }
  2658. if (avail == NULL)
  2659. goto deadlock;
  2660. /* None available, try to steal one or wait for a user to finish */
  2661. list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2662. if (reg->pin_count)
  2663. continue;
  2664. return reg;
  2665. }
  2666. deadlock:
  2667. /* Wait for completion of pending flips which consume fences */
  2668. if (intel_has_pending_fb_unpin(dev))
  2669. return ERR_PTR(-EAGAIN);
  2670. return ERR_PTR(-EDEADLK);
  2671. }
  2672. /**
  2673. * i915_gem_object_get_fence - set up fencing for an object
  2674. * @obj: object to map through a fence reg
  2675. *
  2676. * When mapping objects through the GTT, userspace wants to be able to write
  2677. * to them without having to worry about swizzling if the object is tiled.
  2678. * This function walks the fence regs looking for a free one for @obj,
  2679. * stealing one if it can't find any.
  2680. *
  2681. * It then sets up the reg based on the object's properties: address, pitch
  2682. * and tiling format.
  2683. *
  2684. * For an untiled surface, this removes any existing fence.
  2685. */
  2686. int
  2687. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2688. {
  2689. struct drm_device *dev = obj->base.dev;
  2690. struct drm_i915_private *dev_priv = dev->dev_private;
  2691. bool enable = obj->tiling_mode != I915_TILING_NONE;
  2692. struct drm_i915_fence_reg *reg;
  2693. int ret;
  2694. /* Have we updated the tiling parameters upon the object and so
  2695. * will need to serialise the write to the associated fence register?
  2696. */
  2697. if (obj->fence_dirty) {
  2698. ret = i915_gem_object_wait_fence(obj);
  2699. if (ret)
  2700. return ret;
  2701. }
  2702. /* Just update our place in the LRU if our fence is getting reused. */
  2703. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2704. reg = &dev_priv->fence_regs[obj->fence_reg];
  2705. if (!obj->fence_dirty) {
  2706. list_move_tail(&reg->lru_list,
  2707. &dev_priv->mm.fence_list);
  2708. return 0;
  2709. }
  2710. } else if (enable) {
  2711. reg = i915_find_fence_reg(dev);
  2712. if (IS_ERR(reg))
  2713. return PTR_ERR(reg);
  2714. if (reg->obj) {
  2715. struct drm_i915_gem_object *old = reg->obj;
  2716. ret = i915_gem_object_wait_fence(old);
  2717. if (ret)
  2718. return ret;
  2719. i915_gem_object_fence_lost(old);
  2720. }
  2721. } else
  2722. return 0;
  2723. i915_gem_object_update_fence(obj, reg, enable);
  2724. return 0;
  2725. }
  2726. static bool i915_gem_valid_gtt_space(struct drm_device *dev,
  2727. struct drm_mm_node *gtt_space,
  2728. unsigned long cache_level)
  2729. {
  2730. struct drm_mm_node *other;
  2731. /* On non-LLC machines we have to be careful when putting differing
  2732. * types of snoopable memory together to avoid the prefetcher
  2733. * crossing memory domains and dying.
  2734. */
  2735. if (HAS_LLC(dev))
  2736. return true;
  2737. if (!drm_mm_node_allocated(gtt_space))
  2738. return true;
  2739. if (list_empty(&gtt_space->node_list))
  2740. return true;
  2741. other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2742. if (other->allocated && !other->hole_follows && other->color != cache_level)
  2743. return false;
  2744. other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2745. if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2746. return false;
  2747. return true;
  2748. }
  2749. static void i915_gem_verify_gtt(struct drm_device *dev)
  2750. {
  2751. #if WATCH_GTT
  2752. struct drm_i915_private *dev_priv = dev->dev_private;
  2753. struct drm_i915_gem_object *obj;
  2754. int err = 0;
  2755. list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
  2756. if (obj->gtt_space == NULL) {
  2757. printk(KERN_ERR "object found on GTT list with no space reserved\n");
  2758. err++;
  2759. continue;
  2760. }
  2761. if (obj->cache_level != obj->gtt_space->color) {
  2762. printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
  2763. i915_gem_obj_ggtt_offset(obj),
  2764. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2765. obj->cache_level,
  2766. obj->gtt_space->color);
  2767. err++;
  2768. continue;
  2769. }
  2770. if (!i915_gem_valid_gtt_space(dev,
  2771. obj->gtt_space,
  2772. obj->cache_level)) {
  2773. printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
  2774. i915_gem_obj_ggtt_offset(obj),
  2775. i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
  2776. obj->cache_level);
  2777. err++;
  2778. continue;
  2779. }
  2780. }
  2781. WARN_ON(err);
  2782. #endif
  2783. }
  2784. /**
  2785. * Finds free space in the GTT aperture and binds the object there.
  2786. */
  2787. static struct i915_vma *
  2788. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2789. struct i915_address_space *vm,
  2790. unsigned alignment,
  2791. uint64_t flags)
  2792. {
  2793. struct drm_device *dev = obj->base.dev;
  2794. struct drm_i915_private *dev_priv = dev->dev_private;
  2795. u32 size, fence_size, fence_alignment, unfenced_alignment;
  2796. unsigned long start =
  2797. flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2798. unsigned long end =
  2799. flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
  2800. struct i915_vma *vma;
  2801. int ret;
  2802. fence_size = i915_gem_get_gtt_size(dev,
  2803. obj->base.size,
  2804. obj->tiling_mode);
  2805. fence_alignment = i915_gem_get_gtt_alignment(dev,
  2806. obj->base.size,
  2807. obj->tiling_mode, true);
  2808. unfenced_alignment =
  2809. i915_gem_get_gtt_alignment(dev,
  2810. obj->base.size,
  2811. obj->tiling_mode, false);
  2812. if (alignment == 0)
  2813. alignment = flags & PIN_MAPPABLE ? fence_alignment :
  2814. unfenced_alignment;
  2815. if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  2816. DRM_DEBUG("Invalid object alignment requested %u\n", alignment);
  2817. return ERR_PTR(-EINVAL);
  2818. }
  2819. size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  2820. /* If the object is bigger than the entire aperture, reject it early
  2821. * before evicting everything in a vain attempt to find space.
  2822. */
  2823. if (obj->base.size > end) {
  2824. DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%lu\n",
  2825. obj->base.size,
  2826. flags & PIN_MAPPABLE ? "mappable" : "total",
  2827. end);
  2828. return ERR_PTR(-E2BIG);
  2829. }
  2830. ret = i915_gem_object_get_pages(obj);
  2831. if (ret)
  2832. return ERR_PTR(ret);
  2833. i915_gem_object_pin_pages(obj);
  2834. vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  2835. if (IS_ERR(vma))
  2836. goto err_unpin;
  2837. search_free:
  2838. ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  2839. size, alignment,
  2840. obj->cache_level,
  2841. start, end,
  2842. DRM_MM_SEARCH_DEFAULT,
  2843. DRM_MM_CREATE_DEFAULT);
  2844. if (ret) {
  2845. ret = i915_gem_evict_something(dev, vm, size, alignment,
  2846. obj->cache_level,
  2847. start, end,
  2848. flags);
  2849. if (ret == 0)
  2850. goto search_free;
  2851. goto err_free_vma;
  2852. }
  2853. if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
  2854. obj->cache_level))) {
  2855. ret = -EINVAL;
  2856. goto err_remove_node;
  2857. }
  2858. ret = i915_gem_gtt_prepare_object(obj);
  2859. if (ret)
  2860. goto err_remove_node;
  2861. list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  2862. list_add_tail(&vma->mm_list, &vm->inactive_list);
  2863. if (i915_is_ggtt(vm)) {
  2864. bool mappable, fenceable;
  2865. fenceable = (vma->node.size == fence_size &&
  2866. (vma->node.start & (fence_alignment - 1)) == 0);
  2867. mappable = (vma->node.start + obj->base.size <=
  2868. dev_priv->gtt.mappable_end);
  2869. obj->map_and_fenceable = mappable && fenceable;
  2870. }
  2871. WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  2872. trace_i915_vma_bind(vma, flags);
  2873. vma->bind_vma(vma, obj->cache_level,
  2874. flags & (PIN_MAPPABLE | PIN_GLOBAL) ? GLOBAL_BIND : 0);
  2875. i915_gem_verify_gtt(dev);
  2876. return vma;
  2877. err_remove_node:
  2878. drm_mm_remove_node(&vma->node);
  2879. err_free_vma:
  2880. i915_gem_vma_destroy(vma);
  2881. vma = ERR_PTR(ret);
  2882. err_unpin:
  2883. i915_gem_object_unpin_pages(obj);
  2884. return vma;
  2885. }
  2886. bool
  2887. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  2888. bool force)
  2889. {
  2890. /* If we don't have a page list set up, then we're not pinned
  2891. * to GPU, and we can ignore the cache flush because it'll happen
  2892. * again at bind time.
  2893. */
  2894. if (obj->pages == NULL)
  2895. return false;
  2896. /*
  2897. * Stolen memory is always coherent with the GPU as it is explicitly
  2898. * marked as wc by the system, or the system is cache-coherent.
  2899. */
  2900. if (obj->stolen)
  2901. return false;
  2902. /* If the GPU is snooping the contents of the CPU cache,
  2903. * we do not need to manually clear the CPU cache lines. However,
  2904. * the caches are only snooped when the render cache is
  2905. * flushed/invalidated. As we always have to emit invalidations
  2906. * and flushes when moving into and out of the RENDER domain, correct
  2907. * snooping behaviour occurs naturally as the result of our domain
  2908. * tracking.
  2909. */
  2910. if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  2911. return false;
  2912. trace_i915_gem_object_clflush(obj);
  2913. drm_clflush_sg(obj->pages);
  2914. return true;
  2915. }
  2916. /** Flushes the GTT write domain for the object if it's dirty. */
  2917. static void
  2918. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  2919. {
  2920. uint32_t old_write_domain;
  2921. if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  2922. return;
  2923. /* No actual flushing is required for the GTT write domain. Writes
  2924. * to it immediately go to main memory as far as we know, so there's
  2925. * no chipset flush. It also doesn't land in render cache.
  2926. *
  2927. * However, we do have to enforce the order so that all writes through
  2928. * the GTT land before any writes to the device, such as updates to
  2929. * the GATT itself.
  2930. */
  2931. wmb();
  2932. old_write_domain = obj->base.write_domain;
  2933. obj->base.write_domain = 0;
  2934. trace_i915_gem_object_change_domain(obj,
  2935. obj->base.read_domains,
  2936. old_write_domain);
  2937. }
  2938. /** Flushes the CPU write domain for the object if it's dirty. */
  2939. static void
  2940. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  2941. bool force)
  2942. {
  2943. uint32_t old_write_domain;
  2944. if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  2945. return;
  2946. if (i915_gem_clflush_object(obj, force))
  2947. i915_gem_chipset_flush(obj->base.dev);
  2948. old_write_domain = obj->base.write_domain;
  2949. obj->base.write_domain = 0;
  2950. trace_i915_gem_object_change_domain(obj,
  2951. obj->base.read_domains,
  2952. old_write_domain);
  2953. }
  2954. /**
  2955. * Moves a single object to the GTT read, and possibly write domain.
  2956. *
  2957. * This function returns when the move is complete, including waiting on
  2958. * flushes to occur.
  2959. */
  2960. int
  2961. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  2962. {
  2963. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2964. uint32_t old_write_domain, old_read_domains;
  2965. int ret;
  2966. /* Not valid to be called on unbound objects. */
  2967. if (!i915_gem_obj_bound_any(obj))
  2968. return -EINVAL;
  2969. if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  2970. return 0;
  2971. ret = i915_gem_object_wait_rendering(obj, !write);
  2972. if (ret)
  2973. return ret;
  2974. i915_gem_object_retire(obj);
  2975. i915_gem_object_flush_cpu_write_domain(obj, false);
  2976. /* Serialise direct access to this object with the barriers for
  2977. * coherent writes from the GPU, by effectively invalidating the
  2978. * GTT domain upon first access.
  2979. */
  2980. if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2981. mb();
  2982. old_write_domain = obj->base.write_domain;
  2983. old_read_domains = obj->base.read_domains;
  2984. /* It should now be out of any other write domains, and we can update
  2985. * the domain values for our changes.
  2986. */
  2987. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  2988. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  2989. if (write) {
  2990. obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  2991. obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  2992. obj->dirty = 1;
  2993. }
  2994. trace_i915_gem_object_change_domain(obj,
  2995. old_read_domains,
  2996. old_write_domain);
  2997. /* And bump the LRU for this access */
  2998. if (i915_gem_object_is_inactive(obj)) {
  2999. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3000. if (vma)
  3001. list_move_tail(&vma->mm_list,
  3002. &dev_priv->gtt.base.inactive_list);
  3003. }
  3004. return 0;
  3005. }
  3006. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3007. enum i915_cache_level cache_level)
  3008. {
  3009. struct drm_device *dev = obj->base.dev;
  3010. struct i915_vma *vma, *next;
  3011. int ret;
  3012. if (obj->cache_level == cache_level)
  3013. return 0;
  3014. if (i915_gem_obj_is_pinned(obj)) {
  3015. DRM_DEBUG("can not change the cache level of pinned objects\n");
  3016. return -EBUSY;
  3017. }
  3018. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3019. if (!i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
  3020. ret = i915_vma_unbind(vma);
  3021. if (ret)
  3022. return ret;
  3023. }
  3024. }
  3025. if (i915_gem_obj_bound_any(obj)) {
  3026. ret = i915_gem_object_finish_gpu(obj);
  3027. if (ret)
  3028. return ret;
  3029. i915_gem_object_finish_gtt(obj);
  3030. /* Before SandyBridge, you could not use tiling or fence
  3031. * registers with snooped memory, so relinquish any fences
  3032. * currently pointing to our region in the aperture.
  3033. */
  3034. if (INTEL_INFO(dev)->gen < 6) {
  3035. ret = i915_gem_object_put_fence(obj);
  3036. if (ret)
  3037. return ret;
  3038. }
  3039. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3040. if (drm_mm_node_allocated(&vma->node))
  3041. vma->bind_vma(vma, cache_level,
  3042. obj->has_global_gtt_mapping ? GLOBAL_BIND : 0);
  3043. }
  3044. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3045. vma->node.color = cache_level;
  3046. obj->cache_level = cache_level;
  3047. if (cpu_write_needs_clflush(obj)) {
  3048. u32 old_read_domains, old_write_domain;
  3049. /* If we're coming from LLC cached, then we haven't
  3050. * actually been tracking whether the data is in the
  3051. * CPU cache or not, since we only allow one bit set
  3052. * in obj->write_domain and have been skipping the clflushes.
  3053. * Just set it to the CPU cache for now.
  3054. */
  3055. i915_gem_object_retire(obj);
  3056. WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3057. old_read_domains = obj->base.read_domains;
  3058. old_write_domain = obj->base.write_domain;
  3059. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3060. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3061. trace_i915_gem_object_change_domain(obj,
  3062. old_read_domains,
  3063. old_write_domain);
  3064. }
  3065. i915_gem_verify_gtt(dev);
  3066. return 0;
  3067. }
  3068. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3069. struct drm_file *file)
  3070. {
  3071. struct drm_i915_gem_caching *args = data;
  3072. struct drm_i915_gem_object *obj;
  3073. int ret;
  3074. ret = i915_mutex_lock_interruptible(dev);
  3075. if (ret)
  3076. return ret;
  3077. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3078. if (&obj->base == NULL) {
  3079. ret = -ENOENT;
  3080. goto unlock;
  3081. }
  3082. switch (obj->cache_level) {
  3083. case I915_CACHE_LLC:
  3084. case I915_CACHE_L3_LLC:
  3085. args->caching = I915_CACHING_CACHED;
  3086. break;
  3087. case I915_CACHE_WT:
  3088. args->caching = I915_CACHING_DISPLAY;
  3089. break;
  3090. default:
  3091. args->caching = I915_CACHING_NONE;
  3092. break;
  3093. }
  3094. drm_gem_object_unreference(&obj->base);
  3095. unlock:
  3096. mutex_unlock(&dev->struct_mutex);
  3097. return ret;
  3098. }
  3099. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3100. struct drm_file *file)
  3101. {
  3102. struct drm_i915_gem_caching *args = data;
  3103. struct drm_i915_gem_object *obj;
  3104. enum i915_cache_level level;
  3105. int ret;
  3106. switch (args->caching) {
  3107. case I915_CACHING_NONE:
  3108. level = I915_CACHE_NONE;
  3109. break;
  3110. case I915_CACHING_CACHED:
  3111. level = I915_CACHE_LLC;
  3112. break;
  3113. case I915_CACHING_DISPLAY:
  3114. level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3115. break;
  3116. default:
  3117. return -EINVAL;
  3118. }
  3119. ret = i915_mutex_lock_interruptible(dev);
  3120. if (ret)
  3121. return ret;
  3122. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3123. if (&obj->base == NULL) {
  3124. ret = -ENOENT;
  3125. goto unlock;
  3126. }
  3127. ret = i915_gem_object_set_cache_level(obj, level);
  3128. drm_gem_object_unreference(&obj->base);
  3129. unlock:
  3130. mutex_unlock(&dev->struct_mutex);
  3131. return ret;
  3132. }
  3133. static bool is_pin_display(struct drm_i915_gem_object *obj)
  3134. {
  3135. struct i915_vma *vma;
  3136. if (list_empty(&obj->vma_list))
  3137. return false;
  3138. vma = i915_gem_obj_to_ggtt(obj);
  3139. if (!vma)
  3140. return false;
  3141. /* There are 3 sources that pin objects:
  3142. * 1. The display engine (scanouts, sprites, cursors);
  3143. * 2. Reservations for execbuffer;
  3144. * 3. The user.
  3145. *
  3146. * We can ignore reservations as we hold the struct_mutex and
  3147. * are only called outside of the reservation path. The user
  3148. * can only increment pin_count once, and so if after
  3149. * subtracting the potential reference by the user, any pin_count
  3150. * remains, it must be due to another use by the display engine.
  3151. */
  3152. return vma->pin_count - !!obj->user_pin_count;
  3153. }
  3154. /*
  3155. * Prepare buffer for display plane (scanout, cursors, etc).
  3156. * Can be called from an uninterruptible phase (modesetting) and allows
  3157. * any flushes to be pipelined (for pageflips).
  3158. */
  3159. int
  3160. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3161. u32 alignment,
  3162. struct intel_engine_cs *pipelined)
  3163. {
  3164. u32 old_read_domains, old_write_domain;
  3165. bool was_pin_display;
  3166. int ret;
  3167. if (pipelined != obj->ring) {
  3168. ret = i915_gem_object_sync(obj, pipelined);
  3169. if (ret)
  3170. return ret;
  3171. }
  3172. /* Mark the pin_display early so that we account for the
  3173. * display coherency whilst setting up the cache domains.
  3174. */
  3175. was_pin_display = obj->pin_display;
  3176. obj->pin_display = true;
  3177. /* The display engine is not coherent with the LLC cache on gen6. As
  3178. * a result, we make sure that the pinning that is about to occur is
  3179. * done with uncached PTEs. This is lowest common denominator for all
  3180. * chipsets.
  3181. *
  3182. * However for gen6+, we could do better by using the GFDT bit instead
  3183. * of uncaching, which would allow us to flush all the LLC-cached data
  3184. * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3185. */
  3186. ret = i915_gem_object_set_cache_level(obj,
  3187. HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3188. if (ret)
  3189. goto err_unpin_display;
  3190. /* As the user may map the buffer once pinned in the display plane
  3191. * (e.g. libkms for the bootup splash), we have to ensure that we
  3192. * always use map_and_fenceable for all scanout buffers.
  3193. */
  3194. ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE);
  3195. if (ret)
  3196. goto err_unpin_display;
  3197. i915_gem_object_flush_cpu_write_domain(obj, true);
  3198. old_write_domain = obj->base.write_domain;
  3199. old_read_domains = obj->base.read_domains;
  3200. /* It should now be out of any other write domains, and we can update
  3201. * the domain values for our changes.
  3202. */
  3203. obj->base.write_domain = 0;
  3204. obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3205. trace_i915_gem_object_change_domain(obj,
  3206. old_read_domains,
  3207. old_write_domain);
  3208. return 0;
  3209. err_unpin_display:
  3210. WARN_ON(was_pin_display != is_pin_display(obj));
  3211. obj->pin_display = was_pin_display;
  3212. return ret;
  3213. }
  3214. void
  3215. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
  3216. {
  3217. i915_gem_object_ggtt_unpin(obj);
  3218. obj->pin_display = is_pin_display(obj);
  3219. }
  3220. int
  3221. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  3222. {
  3223. int ret;
  3224. if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  3225. return 0;
  3226. ret = i915_gem_object_wait_rendering(obj, false);
  3227. if (ret)
  3228. return ret;
  3229. /* Ensure that we invalidate the GPU's caches and TLBs. */
  3230. obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  3231. return 0;
  3232. }
  3233. /**
  3234. * Moves a single object to the CPU read, and possibly write domain.
  3235. *
  3236. * This function returns when the move is complete, including waiting on
  3237. * flushes to occur.
  3238. */
  3239. int
  3240. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3241. {
  3242. uint32_t old_write_domain, old_read_domains;
  3243. int ret;
  3244. if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3245. return 0;
  3246. ret = i915_gem_object_wait_rendering(obj, !write);
  3247. if (ret)
  3248. return ret;
  3249. i915_gem_object_retire(obj);
  3250. i915_gem_object_flush_gtt_write_domain(obj);
  3251. old_write_domain = obj->base.write_domain;
  3252. old_read_domains = obj->base.read_domains;
  3253. /* Flush the CPU cache if it's still invalid. */
  3254. if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3255. i915_gem_clflush_object(obj, false);
  3256. obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3257. }
  3258. /* It should now be out of any other write domains, and we can update
  3259. * the domain values for our changes.
  3260. */
  3261. BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3262. /* If we're writing through the CPU, then the GPU read domains will
  3263. * need to be invalidated at next use.
  3264. */
  3265. if (write) {
  3266. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3267. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3268. }
  3269. trace_i915_gem_object_change_domain(obj,
  3270. old_read_domains,
  3271. old_write_domain);
  3272. return 0;
  3273. }
  3274. /* Throttle our rendering by waiting until the ring has completed our requests
  3275. * emitted over 20 msec ago.
  3276. *
  3277. * Note that if we were to use the current jiffies each time around the loop,
  3278. * we wouldn't escape the function with any frames outstanding if the time to
  3279. * render a frame was over 20ms.
  3280. *
  3281. * This should get us reasonable parallelism between CPU and GPU but also
  3282. * relatively low latency when blocking on a particular request to finish.
  3283. */
  3284. static int
  3285. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3286. {
  3287. struct drm_i915_private *dev_priv = dev->dev_private;
  3288. struct drm_i915_file_private *file_priv = file->driver_priv;
  3289. unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3290. struct drm_i915_gem_request *request;
  3291. struct intel_engine_cs *ring = NULL;
  3292. unsigned reset_counter;
  3293. u32 seqno = 0;
  3294. int ret;
  3295. ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3296. if (ret)
  3297. return ret;
  3298. ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3299. if (ret)
  3300. return ret;
  3301. spin_lock(&file_priv->mm.lock);
  3302. list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3303. if (time_after_eq(request->emitted_jiffies, recent_enough))
  3304. break;
  3305. ring = request->ring;
  3306. seqno = request->seqno;
  3307. }
  3308. reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3309. spin_unlock(&file_priv->mm.lock);
  3310. if (seqno == 0)
  3311. return 0;
  3312. ret = __wait_seqno(ring, seqno, reset_counter, true, NULL, NULL);
  3313. if (ret == 0)
  3314. queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3315. return ret;
  3316. }
  3317. static bool
  3318. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3319. {
  3320. struct drm_i915_gem_object *obj = vma->obj;
  3321. if (alignment &&
  3322. vma->node.start & (alignment - 1))
  3323. return true;
  3324. if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3325. return true;
  3326. if (flags & PIN_OFFSET_BIAS &&
  3327. vma->node.start < (flags & PIN_OFFSET_MASK))
  3328. return true;
  3329. return false;
  3330. }
  3331. int
  3332. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3333. struct i915_address_space *vm,
  3334. uint32_t alignment,
  3335. uint64_t flags)
  3336. {
  3337. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3338. struct i915_vma *vma;
  3339. int ret;
  3340. if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3341. return -ENODEV;
  3342. if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3343. return -EINVAL;
  3344. vma = i915_gem_obj_to_vma(obj, vm);
  3345. if (vma) {
  3346. if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3347. return -EBUSY;
  3348. if (i915_vma_misplaced(vma, alignment, flags)) {
  3349. WARN(vma->pin_count,
  3350. "bo is already pinned with incorrect alignment:"
  3351. " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3352. " obj->map_and_fenceable=%d\n",
  3353. i915_gem_obj_offset(obj, vm), alignment,
  3354. !!(flags & PIN_MAPPABLE),
  3355. obj->map_and_fenceable);
  3356. ret = i915_vma_unbind(vma);
  3357. if (ret)
  3358. return ret;
  3359. vma = NULL;
  3360. }
  3361. }
  3362. if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3363. vma = i915_gem_object_bind_to_vm(obj, vm, alignment, flags);
  3364. if (IS_ERR(vma))
  3365. return PTR_ERR(vma);
  3366. }
  3367. if (flags & PIN_GLOBAL && !obj->has_global_gtt_mapping)
  3368. vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
  3369. vma->pin_count++;
  3370. if (flags & PIN_MAPPABLE)
  3371. obj->pin_mappable |= true;
  3372. return 0;
  3373. }
  3374. void
  3375. i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj)
  3376. {
  3377. struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3378. BUG_ON(!vma);
  3379. BUG_ON(vma->pin_count == 0);
  3380. BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3381. if (--vma->pin_count == 0)
  3382. obj->pin_mappable = false;
  3383. }
  3384. bool
  3385. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  3386. {
  3387. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3388. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3389. struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  3390. WARN_ON(!ggtt_vma ||
  3391. dev_priv->fence_regs[obj->fence_reg].pin_count >
  3392. ggtt_vma->pin_count);
  3393. dev_priv->fence_regs[obj->fence_reg].pin_count++;
  3394. return true;
  3395. } else
  3396. return false;
  3397. }
  3398. void
  3399. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  3400. {
  3401. if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3402. struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3403. WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  3404. dev_priv->fence_regs[obj->fence_reg].pin_count--;
  3405. }
  3406. }
  3407. int
  3408. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3409. struct drm_file *file)
  3410. {
  3411. struct drm_i915_gem_pin *args = data;
  3412. struct drm_i915_gem_object *obj;
  3413. int ret;
  3414. if (INTEL_INFO(dev)->gen >= 6)
  3415. return -ENODEV;
  3416. ret = i915_mutex_lock_interruptible(dev);
  3417. if (ret)
  3418. return ret;
  3419. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3420. if (&obj->base == NULL) {
  3421. ret = -ENOENT;
  3422. goto unlock;
  3423. }
  3424. if (obj->madv != I915_MADV_WILLNEED) {
  3425. DRM_DEBUG("Attempting to pin a purgeable buffer\n");
  3426. ret = -EFAULT;
  3427. goto out;
  3428. }
  3429. if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3430. DRM_DEBUG("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3431. args->handle);
  3432. ret = -EINVAL;
  3433. goto out;
  3434. }
  3435. if (obj->user_pin_count == ULONG_MAX) {
  3436. ret = -EBUSY;
  3437. goto out;
  3438. }
  3439. if (obj->user_pin_count == 0) {
  3440. ret = i915_gem_obj_ggtt_pin(obj, args->alignment, PIN_MAPPABLE);
  3441. if (ret)
  3442. goto out;
  3443. }
  3444. obj->user_pin_count++;
  3445. obj->pin_filp = file;
  3446. args->offset = i915_gem_obj_ggtt_offset(obj);
  3447. out:
  3448. drm_gem_object_unreference(&obj->base);
  3449. unlock:
  3450. mutex_unlock(&dev->struct_mutex);
  3451. return ret;
  3452. }
  3453. int
  3454. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3455. struct drm_file *file)
  3456. {
  3457. struct drm_i915_gem_pin *args = data;
  3458. struct drm_i915_gem_object *obj;
  3459. int ret;
  3460. ret = i915_mutex_lock_interruptible(dev);
  3461. if (ret)
  3462. return ret;
  3463. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3464. if (&obj->base == NULL) {
  3465. ret = -ENOENT;
  3466. goto unlock;
  3467. }
  3468. if (obj->pin_filp != file) {
  3469. DRM_DEBUG("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3470. args->handle);
  3471. ret = -EINVAL;
  3472. goto out;
  3473. }
  3474. obj->user_pin_count--;
  3475. if (obj->user_pin_count == 0) {
  3476. obj->pin_filp = NULL;
  3477. i915_gem_object_ggtt_unpin(obj);
  3478. }
  3479. out:
  3480. drm_gem_object_unreference(&obj->base);
  3481. unlock:
  3482. mutex_unlock(&dev->struct_mutex);
  3483. return ret;
  3484. }
  3485. int
  3486. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3487. struct drm_file *file)
  3488. {
  3489. struct drm_i915_gem_busy *args = data;
  3490. struct drm_i915_gem_object *obj;
  3491. int ret;
  3492. ret = i915_mutex_lock_interruptible(dev);
  3493. if (ret)
  3494. return ret;
  3495. obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3496. if (&obj->base == NULL) {
  3497. ret = -ENOENT;
  3498. goto unlock;
  3499. }
  3500. /* Count all active objects as busy, even if they are currently not used
  3501. * by the gpu. Users of this interface expect objects to eventually
  3502. * become non-busy without any further actions, therefore emit any
  3503. * necessary flushes here.
  3504. */
  3505. ret = i915_gem_object_flush_active(obj);
  3506. args->busy = obj->active;
  3507. if (obj->ring) {
  3508. BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3509. args->busy |= intel_ring_flag(obj->ring) << 16;
  3510. }
  3511. drm_gem_object_unreference(&obj->base);
  3512. unlock:
  3513. mutex_unlock(&dev->struct_mutex);
  3514. return ret;
  3515. }
  3516. int
  3517. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3518. struct drm_file *file_priv)
  3519. {
  3520. return i915_gem_ring_throttle(dev, file_priv);
  3521. }
  3522. int
  3523. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3524. struct drm_file *file_priv)
  3525. {
  3526. struct drm_i915_gem_madvise *args = data;
  3527. struct drm_i915_gem_object *obj;
  3528. int ret;
  3529. switch (args->madv) {
  3530. case I915_MADV_DONTNEED:
  3531. case I915_MADV_WILLNEED:
  3532. break;
  3533. default:
  3534. return -EINVAL;
  3535. }
  3536. ret = i915_mutex_lock_interruptible(dev);
  3537. if (ret)
  3538. return ret;
  3539. obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3540. if (&obj->base == NULL) {
  3541. ret = -ENOENT;
  3542. goto unlock;
  3543. }
  3544. if (i915_gem_obj_is_pinned(obj)) {
  3545. ret = -EINVAL;
  3546. goto out;
  3547. }
  3548. if (obj->madv != __I915_MADV_PURGED)
  3549. obj->madv = args->madv;
  3550. /* if the object is no longer attached, discard its backing storage */
  3551. if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3552. i915_gem_object_truncate(obj);
  3553. args->retained = obj->madv != __I915_MADV_PURGED;
  3554. out:
  3555. drm_gem_object_unreference(&obj->base);
  3556. unlock:
  3557. mutex_unlock(&dev->struct_mutex);
  3558. return ret;
  3559. }
  3560. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3561. const struct drm_i915_gem_object_ops *ops)
  3562. {
  3563. INIT_LIST_HEAD(&obj->global_list);
  3564. INIT_LIST_HEAD(&obj->ring_list);
  3565. INIT_LIST_HEAD(&obj->obj_exec_link);
  3566. INIT_LIST_HEAD(&obj->vma_list);
  3567. obj->ops = ops;
  3568. obj->fence_reg = I915_FENCE_REG_NONE;
  3569. obj->madv = I915_MADV_WILLNEED;
  3570. /* Avoid an unnecessary call to unbind on the first bind. */
  3571. obj->map_and_fenceable = true;
  3572. i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3573. }
  3574. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3575. .get_pages = i915_gem_object_get_pages_gtt,
  3576. .put_pages = i915_gem_object_put_pages_gtt,
  3577. };
  3578. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3579. size_t size)
  3580. {
  3581. struct drm_i915_gem_object *obj;
  3582. struct address_space *mapping;
  3583. gfp_t mask;
  3584. obj = i915_gem_object_alloc(dev);
  3585. if (obj == NULL)
  3586. return NULL;
  3587. if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3588. i915_gem_object_free(obj);
  3589. return NULL;
  3590. }
  3591. mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
  3592. if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
  3593. /* 965gm cannot relocate objects above 4GiB. */
  3594. mask &= ~__GFP_HIGHMEM;
  3595. mask |= __GFP_DMA32;
  3596. }
  3597. mapping = file_inode(obj->base.filp)->i_mapping;
  3598. mapping_set_gfp_mask(mapping, mask);
  3599. i915_gem_object_init(obj, &i915_gem_object_ops);
  3600. obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3601. obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3602. if (HAS_LLC(dev)) {
  3603. /* On some devices, we can have the GPU use the LLC (the CPU
  3604. * cache) for about a 10% performance improvement
  3605. * compared to uncached. Graphics requests other than
  3606. * display scanout are coherent with the CPU in
  3607. * accessing this cache. This means in this mode we
  3608. * don't need to clflush on the CPU side, and on the
  3609. * GPU side we only need to flush internal caches to
  3610. * get data visible to the CPU.
  3611. *
  3612. * However, we maintain the display planes as UC, and so
  3613. * need to rebind when first used as such.
  3614. */
  3615. obj->cache_level = I915_CACHE_LLC;
  3616. } else
  3617. obj->cache_level = I915_CACHE_NONE;
  3618. trace_i915_gem_object_create(obj);
  3619. return obj;
  3620. }
  3621. static bool discard_backing_storage(struct drm_i915_gem_object *obj)
  3622. {
  3623. /* If we are the last user of the backing storage (be it shmemfs
  3624. * pages or stolen etc), we know that the pages are going to be
  3625. * immediately released. In this case, we can then skip copying
  3626. * back the contents from the GPU.
  3627. */
  3628. if (obj->madv != I915_MADV_WILLNEED)
  3629. return false;
  3630. if (obj->base.filp == NULL)
  3631. return true;
  3632. /* At first glance, this looks racy, but then again so would be
  3633. * userspace racing mmap against close. However, the first external
  3634. * reference to the filp can only be obtained through the
  3635. * i915_gem_mmap_ioctl() which safeguards us against the user
  3636. * acquiring such a reference whilst we are in the middle of
  3637. * freeing the object.
  3638. */
  3639. return atomic_long_read(&obj->base.filp->f_count) == 1;
  3640. }
  3641. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  3642. {
  3643. struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  3644. struct drm_device *dev = obj->base.dev;
  3645. struct drm_i915_private *dev_priv = dev->dev_private;
  3646. struct i915_vma *vma, *next;
  3647. intel_runtime_pm_get(dev_priv);
  3648. trace_i915_gem_object_destroy(obj);
  3649. list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3650. int ret;
  3651. vma->pin_count = 0;
  3652. ret = i915_vma_unbind(vma);
  3653. if (WARN_ON(ret == -ERESTARTSYS)) {
  3654. bool was_interruptible;
  3655. was_interruptible = dev_priv->mm.interruptible;
  3656. dev_priv->mm.interruptible = false;
  3657. WARN_ON(i915_vma_unbind(vma));
  3658. dev_priv->mm.interruptible = was_interruptible;
  3659. }
  3660. }
  3661. i915_gem_object_detach_phys(obj);
  3662. /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  3663. * before progressing. */
  3664. if (obj->stolen)
  3665. i915_gem_object_unpin_pages(obj);
  3666. if (WARN_ON(obj->pages_pin_count))
  3667. obj->pages_pin_count = 0;
  3668. if (discard_backing_storage(obj))
  3669. obj->madv = I915_MADV_DONTNEED;
  3670. i915_gem_object_put_pages(obj);
  3671. i915_gem_object_free_mmap_offset(obj);
  3672. i915_gem_object_release_stolen(obj);
  3673. BUG_ON(obj->pages);
  3674. if (obj->base.import_attach)
  3675. drm_prime_gem_destroy(&obj->base, NULL);
  3676. if (obj->ops->release)
  3677. obj->ops->release(obj);
  3678. drm_gem_object_release(&obj->base);
  3679. i915_gem_info_remove_obj(dev_priv, obj->base.size);
  3680. kfree(obj->bit_17);
  3681. i915_gem_object_free(obj);
  3682. intel_runtime_pm_put(dev_priv);
  3683. }
  3684. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  3685. struct i915_address_space *vm)
  3686. {
  3687. struct i915_vma *vma;
  3688. list_for_each_entry(vma, &obj->vma_list, vma_link)
  3689. if (vma->vm == vm)
  3690. return vma;
  3691. return NULL;
  3692. }
  3693. void i915_gem_vma_destroy(struct i915_vma *vma)
  3694. {
  3695. WARN_ON(vma->node.allocated);
  3696. /* Keep the vma as a placeholder in the execbuffer reservation lists */
  3697. if (!list_empty(&vma->exec_list))
  3698. return;
  3699. list_del(&vma->vma_link);
  3700. kfree(vma);
  3701. }
  3702. static void
  3703. i915_gem_stop_ringbuffers(struct drm_device *dev)
  3704. {
  3705. struct drm_i915_private *dev_priv = dev->dev_private;
  3706. struct intel_engine_cs *ring;
  3707. int i;
  3708. for_each_ring(ring, dev_priv, i)
  3709. intel_stop_ring_buffer(ring);
  3710. }
  3711. int
  3712. i915_gem_suspend(struct drm_device *dev)
  3713. {
  3714. struct drm_i915_private *dev_priv = dev->dev_private;
  3715. int ret = 0;
  3716. mutex_lock(&dev->struct_mutex);
  3717. if (dev_priv->ums.mm_suspended)
  3718. goto err;
  3719. ret = i915_gpu_idle(dev);
  3720. if (ret)
  3721. goto err;
  3722. i915_gem_retire_requests(dev);
  3723. /* Under UMS, be paranoid and evict. */
  3724. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3725. i915_gem_evict_everything(dev);
  3726. i915_kernel_lost_context(dev);
  3727. i915_gem_stop_ringbuffers(dev);
  3728. /* Hack! Don't let anybody do execbuf while we don't control the chip.
  3729. * We need to replace this with a semaphore, or something.
  3730. * And not confound ums.mm_suspended!
  3731. */
  3732. dev_priv->ums.mm_suspended = !drm_core_check_feature(dev,
  3733. DRIVER_MODESET);
  3734. mutex_unlock(&dev->struct_mutex);
  3735. del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  3736. cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  3737. cancel_delayed_work_sync(&dev_priv->mm.idle_work);
  3738. return 0;
  3739. err:
  3740. mutex_unlock(&dev->struct_mutex);
  3741. return ret;
  3742. }
  3743. int i915_gem_l3_remap(struct intel_engine_cs *ring, int slice)
  3744. {
  3745. struct drm_device *dev = ring->dev;
  3746. struct drm_i915_private *dev_priv = dev->dev_private;
  3747. u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  3748. u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  3749. int i, ret;
  3750. if (!HAS_L3_DPF(dev) || !remap_info)
  3751. return 0;
  3752. ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  3753. if (ret)
  3754. return ret;
  3755. /*
  3756. * Note: We do not worry about the concurrent register cacheline hang
  3757. * here because no other code should access these registers other than
  3758. * at initialization time.
  3759. */
  3760. for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  3761. intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  3762. intel_ring_emit(ring, reg_base + i);
  3763. intel_ring_emit(ring, remap_info[i/4]);
  3764. }
  3765. intel_ring_advance(ring);
  3766. return ret;
  3767. }
  3768. void i915_gem_init_swizzling(struct drm_device *dev)
  3769. {
  3770. struct drm_i915_private *dev_priv = dev->dev_private;
  3771. if (INTEL_INFO(dev)->gen < 5 ||
  3772. dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  3773. return;
  3774. I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  3775. DISP_TILE_SURFACE_SWIZZLING);
  3776. if (IS_GEN5(dev))
  3777. return;
  3778. I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  3779. if (IS_GEN6(dev))
  3780. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  3781. else if (IS_GEN7(dev))
  3782. I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  3783. else if (IS_GEN8(dev))
  3784. I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  3785. else
  3786. BUG();
  3787. }
  3788. static bool
  3789. intel_enable_blt(struct drm_device *dev)
  3790. {
  3791. if (!HAS_BLT(dev))
  3792. return false;
  3793. /* The blitter was dysfunctional on early prototypes */
  3794. if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  3795. DRM_INFO("BLT not supported on this pre-production hardware;"
  3796. " graphics performance will be degraded.\n");
  3797. return false;
  3798. }
  3799. return true;
  3800. }
  3801. static int i915_gem_init_rings(struct drm_device *dev)
  3802. {
  3803. struct drm_i915_private *dev_priv = dev->dev_private;
  3804. int ret;
  3805. ret = intel_init_render_ring_buffer(dev);
  3806. if (ret)
  3807. return ret;
  3808. if (HAS_BSD(dev)) {
  3809. ret = intel_init_bsd_ring_buffer(dev);
  3810. if (ret)
  3811. goto cleanup_render_ring;
  3812. }
  3813. if (intel_enable_blt(dev)) {
  3814. ret = intel_init_blt_ring_buffer(dev);
  3815. if (ret)
  3816. goto cleanup_bsd_ring;
  3817. }
  3818. if (HAS_VEBOX(dev)) {
  3819. ret = intel_init_vebox_ring_buffer(dev);
  3820. if (ret)
  3821. goto cleanup_blt_ring;
  3822. }
  3823. if (HAS_BSD2(dev)) {
  3824. ret = intel_init_bsd2_ring_buffer(dev);
  3825. if (ret)
  3826. goto cleanup_vebox_ring;
  3827. }
  3828. ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  3829. if (ret)
  3830. goto cleanup_bsd2_ring;
  3831. return 0;
  3832. cleanup_bsd2_ring:
  3833. intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
  3834. cleanup_vebox_ring:
  3835. intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  3836. cleanup_blt_ring:
  3837. intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  3838. cleanup_bsd_ring:
  3839. intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  3840. cleanup_render_ring:
  3841. intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  3842. return ret;
  3843. }
  3844. int
  3845. i915_gem_init_hw(struct drm_device *dev)
  3846. {
  3847. struct drm_i915_private *dev_priv = dev->dev_private;
  3848. int ret, i;
  3849. if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  3850. return -EIO;
  3851. if (dev_priv->ellc_size)
  3852. I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  3853. if (IS_HASWELL(dev))
  3854. I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  3855. LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  3856. if (HAS_PCH_NOP(dev)) {
  3857. if (IS_IVYBRIDGE(dev)) {
  3858. u32 temp = I915_READ(GEN7_MSG_CTL);
  3859. temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  3860. I915_WRITE(GEN7_MSG_CTL, temp);
  3861. } else if (INTEL_INFO(dev)->gen >= 7) {
  3862. u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  3863. temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  3864. I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  3865. }
  3866. }
  3867. i915_gem_init_swizzling(dev);
  3868. ret = i915_gem_init_rings(dev);
  3869. if (ret)
  3870. return ret;
  3871. for (i = 0; i < NUM_L3_SLICES(dev); i++)
  3872. i915_gem_l3_remap(&dev_priv->ring[RCS], i);
  3873. /*
  3874. * XXX: Contexts should only be initialized once. Doing a switch to the
  3875. * default context switch however is something we'd like to do after
  3876. * reset or thaw (the latter may not actually be necessary for HW, but
  3877. * goes with our code better). Context switching requires rings (for
  3878. * the do_switch), but before enabling PPGTT. So don't move this.
  3879. */
  3880. ret = i915_gem_context_enable(dev_priv);
  3881. if (ret && ret != -EIO) {
  3882. DRM_ERROR("Context enable failed %d\n", ret);
  3883. i915_gem_cleanup_ringbuffer(dev);
  3884. }
  3885. return ret;
  3886. }
  3887. int i915_gem_init(struct drm_device *dev)
  3888. {
  3889. struct drm_i915_private *dev_priv = dev->dev_private;
  3890. int ret;
  3891. mutex_lock(&dev->struct_mutex);
  3892. if (IS_VALLEYVIEW(dev)) {
  3893. /* VLVA0 (potential hack), BIOS isn't actually waking us */
  3894. I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  3895. if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  3896. VLV_GTLC_ALLOWWAKEACK), 10))
  3897. DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  3898. }
  3899. i915_gem_init_userptr(dev);
  3900. i915_gem_init_global_gtt(dev);
  3901. ret = i915_gem_context_init(dev);
  3902. if (ret) {
  3903. mutex_unlock(&dev->struct_mutex);
  3904. return ret;
  3905. }
  3906. ret = i915_gem_init_hw(dev);
  3907. if (ret == -EIO) {
  3908. /* Allow ring initialisation to fail by marking the GPU as
  3909. * wedged. But we only want to do this where the GPU is angry,
  3910. * for all other failure, such as an allocation failure, bail.
  3911. */
  3912. DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  3913. atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  3914. ret = 0;
  3915. }
  3916. mutex_unlock(&dev->struct_mutex);
  3917. /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
  3918. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  3919. dev_priv->dri1.allow_batchbuffer = 1;
  3920. return ret;
  3921. }
  3922. void
  3923. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  3924. {
  3925. struct drm_i915_private *dev_priv = dev->dev_private;
  3926. struct intel_engine_cs *ring;
  3927. int i;
  3928. for_each_ring(ring, dev_priv, i)
  3929. intel_cleanup_ring_buffer(ring);
  3930. }
  3931. int
  3932. i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
  3933. struct drm_file *file_priv)
  3934. {
  3935. struct drm_i915_private *dev_priv = dev->dev_private;
  3936. int ret;
  3937. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3938. return 0;
  3939. if (i915_reset_in_progress(&dev_priv->gpu_error)) {
  3940. DRM_ERROR("Reenabling wedged hardware, good luck\n");
  3941. atomic_set(&dev_priv->gpu_error.reset_counter, 0);
  3942. }
  3943. mutex_lock(&dev->struct_mutex);
  3944. dev_priv->ums.mm_suspended = 0;
  3945. ret = i915_gem_init_hw(dev);
  3946. if (ret != 0) {
  3947. mutex_unlock(&dev->struct_mutex);
  3948. return ret;
  3949. }
  3950. BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
  3951. ret = drm_irq_install(dev, dev->pdev->irq);
  3952. if (ret)
  3953. goto cleanup_ringbuffer;
  3954. mutex_unlock(&dev->struct_mutex);
  3955. return 0;
  3956. cleanup_ringbuffer:
  3957. i915_gem_cleanup_ringbuffer(dev);
  3958. dev_priv->ums.mm_suspended = 1;
  3959. mutex_unlock(&dev->struct_mutex);
  3960. return ret;
  3961. }
  3962. int
  3963. i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
  3964. struct drm_file *file_priv)
  3965. {
  3966. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3967. return 0;
  3968. mutex_lock(&dev->struct_mutex);
  3969. drm_irq_uninstall(dev);
  3970. mutex_unlock(&dev->struct_mutex);
  3971. return i915_gem_suspend(dev);
  3972. }
  3973. void
  3974. i915_gem_lastclose(struct drm_device *dev)
  3975. {
  3976. int ret;
  3977. if (drm_core_check_feature(dev, DRIVER_MODESET))
  3978. return;
  3979. ret = i915_gem_suspend(dev);
  3980. if (ret)
  3981. DRM_ERROR("failed to idle hardware: %d\n", ret);
  3982. }
  3983. static void
  3984. init_ring_lists(struct intel_engine_cs *ring)
  3985. {
  3986. INIT_LIST_HEAD(&ring->active_list);
  3987. INIT_LIST_HEAD(&ring->request_list);
  3988. }
  3989. void i915_init_vm(struct drm_i915_private *dev_priv,
  3990. struct i915_address_space *vm)
  3991. {
  3992. if (!i915_is_ggtt(vm))
  3993. drm_mm_init(&vm->mm, vm->start, vm->total);
  3994. vm->dev = dev_priv->dev;
  3995. INIT_LIST_HEAD(&vm->active_list);
  3996. INIT_LIST_HEAD(&vm->inactive_list);
  3997. INIT_LIST_HEAD(&vm->global_link);
  3998. list_add_tail(&vm->global_link, &dev_priv->vm_list);
  3999. }
  4000. void
  4001. i915_gem_load(struct drm_device *dev)
  4002. {
  4003. struct drm_i915_private *dev_priv = dev->dev_private;
  4004. int i;
  4005. dev_priv->slab =
  4006. kmem_cache_create("i915_gem_object",
  4007. sizeof(struct drm_i915_gem_object), 0,
  4008. SLAB_HWCACHE_ALIGN,
  4009. NULL);
  4010. INIT_LIST_HEAD(&dev_priv->vm_list);
  4011. i915_init_vm(dev_priv, &dev_priv->gtt.base);
  4012. INIT_LIST_HEAD(&dev_priv->context_list);
  4013. INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4014. INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4015. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4016. for (i = 0; i < I915_NUM_RINGS; i++)
  4017. init_ring_lists(&dev_priv->ring[i]);
  4018. for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4019. INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4020. INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4021. i915_gem_retire_work_handler);
  4022. INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4023. i915_gem_idle_work_handler);
  4024. init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4025. /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4026. if (!drm_core_check_feature(dev, DRIVER_MODESET) && IS_GEN3(dev)) {
  4027. I915_WRITE(MI_ARB_STATE,
  4028. _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  4029. }
  4030. dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4031. /* Old X drivers will take 0-2 for front, back, depth buffers */
  4032. if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4033. dev_priv->fence_reg_start = 3;
  4034. if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4035. dev_priv->num_fence_regs = 32;
  4036. else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4037. dev_priv->num_fence_regs = 16;
  4038. else
  4039. dev_priv->num_fence_regs = 8;
  4040. /* Initialize fence registers to zero */
  4041. INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4042. i915_gem_restore_fences(dev);
  4043. i915_gem_detect_bit_6_swizzle(dev);
  4044. init_waitqueue_head(&dev_priv->pending_flip_queue);
  4045. dev_priv->mm.interruptible = true;
  4046. dev_priv->mm.shrinker.scan_objects = i915_gem_shrinker_scan;
  4047. dev_priv->mm.shrinker.count_objects = i915_gem_shrinker_count;
  4048. dev_priv->mm.shrinker.seeks = DEFAULT_SEEKS;
  4049. register_shrinker(&dev_priv->mm.shrinker);
  4050. dev_priv->mm.oom_notifier.notifier_call = i915_gem_shrinker_oom;
  4051. register_oom_notifier(&dev_priv->mm.oom_notifier);
  4052. }
  4053. void i915_gem_release(struct drm_device *dev, struct drm_file *file)
  4054. {
  4055. struct drm_i915_file_private *file_priv = file->driver_priv;
  4056. cancel_delayed_work_sync(&file_priv->mm.idle_work);
  4057. /* Clean up our request list when the client is going away, so that
  4058. * later retire_requests won't dereference our soon-to-be-gone
  4059. * file_priv.
  4060. */
  4061. spin_lock(&file_priv->mm.lock);
  4062. while (!list_empty(&file_priv->mm.request_list)) {
  4063. struct drm_i915_gem_request *request;
  4064. request = list_first_entry(&file_priv->mm.request_list,
  4065. struct drm_i915_gem_request,
  4066. client_list);
  4067. list_del(&request->client_list);
  4068. request->file_priv = NULL;
  4069. }
  4070. spin_unlock(&file_priv->mm.lock);
  4071. }
  4072. static void
  4073. i915_gem_file_idle_work_handler(struct work_struct *work)
  4074. {
  4075. struct drm_i915_file_private *file_priv =
  4076. container_of(work, typeof(*file_priv), mm.idle_work.work);
  4077. atomic_set(&file_priv->rps_wait_boost, false);
  4078. }
  4079. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4080. {
  4081. struct drm_i915_file_private *file_priv;
  4082. int ret;
  4083. DRM_DEBUG_DRIVER("\n");
  4084. file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4085. if (!file_priv)
  4086. return -ENOMEM;
  4087. file->driver_priv = file_priv;
  4088. file_priv->dev_priv = dev->dev_private;
  4089. file_priv->file = file;
  4090. spin_lock_init(&file_priv->mm.lock);
  4091. INIT_LIST_HEAD(&file_priv->mm.request_list);
  4092. INIT_DELAYED_WORK(&file_priv->mm.idle_work,
  4093. i915_gem_file_idle_work_handler);
  4094. ret = i915_gem_context_open(dev, file);
  4095. if (ret)
  4096. kfree(file_priv);
  4097. return ret;
  4098. }
  4099. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  4100. {
  4101. if (!mutex_is_locked(mutex))
  4102. return false;
  4103. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  4104. return mutex->owner == task;
  4105. #else
  4106. /* Since UP may be pre-empted, we cannot assume that we own the lock */
  4107. return false;
  4108. #endif
  4109. }
  4110. static bool i915_gem_shrinker_lock(struct drm_device *dev, bool *unlock)
  4111. {
  4112. if (!mutex_trylock(&dev->struct_mutex)) {
  4113. if (!mutex_is_locked_by(&dev->struct_mutex, current))
  4114. return false;
  4115. if (to_i915(dev)->mm.shrinker_no_lock_stealing)
  4116. return false;
  4117. *unlock = false;
  4118. } else
  4119. *unlock = true;
  4120. return true;
  4121. }
  4122. static int num_vma_bound(struct drm_i915_gem_object *obj)
  4123. {
  4124. struct i915_vma *vma;
  4125. int count = 0;
  4126. list_for_each_entry(vma, &obj->vma_list, vma_link)
  4127. if (drm_mm_node_allocated(&vma->node))
  4128. count++;
  4129. return count;
  4130. }
  4131. static unsigned long
  4132. i915_gem_shrinker_count(struct shrinker *shrinker, struct shrink_control *sc)
  4133. {
  4134. struct drm_i915_private *dev_priv =
  4135. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4136. struct drm_device *dev = dev_priv->dev;
  4137. struct drm_i915_gem_object *obj;
  4138. unsigned long count;
  4139. bool unlock;
  4140. if (!i915_gem_shrinker_lock(dev, &unlock))
  4141. return 0;
  4142. count = 0;
  4143. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
  4144. if (obj->pages_pin_count == 0)
  4145. count += obj->base.size >> PAGE_SHIFT;
  4146. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4147. if (!i915_gem_obj_is_pinned(obj) &&
  4148. obj->pages_pin_count == num_vma_bound(obj))
  4149. count += obj->base.size >> PAGE_SHIFT;
  4150. }
  4151. if (unlock)
  4152. mutex_unlock(&dev->struct_mutex);
  4153. return count;
  4154. }
  4155. /* All the new VM stuff */
  4156. unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4157. struct i915_address_space *vm)
  4158. {
  4159. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4160. struct i915_vma *vma;
  4161. if (!dev_priv->mm.aliasing_ppgtt ||
  4162. vm == &dev_priv->mm.aliasing_ppgtt->base)
  4163. vm = &dev_priv->gtt.base;
  4164. BUG_ON(list_empty(&o->vma_list));
  4165. list_for_each_entry(vma, &o->vma_list, vma_link) {
  4166. if (vma->vm == vm)
  4167. return vma->node.start;
  4168. }
  4169. return -1;
  4170. }
  4171. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4172. struct i915_address_space *vm)
  4173. {
  4174. struct i915_vma *vma;
  4175. list_for_each_entry(vma, &o->vma_list, vma_link)
  4176. if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4177. return true;
  4178. return false;
  4179. }
  4180. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4181. {
  4182. struct i915_vma *vma;
  4183. list_for_each_entry(vma, &o->vma_list, vma_link)
  4184. if (drm_mm_node_allocated(&vma->node))
  4185. return true;
  4186. return false;
  4187. }
  4188. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4189. struct i915_address_space *vm)
  4190. {
  4191. struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4192. struct i915_vma *vma;
  4193. if (!dev_priv->mm.aliasing_ppgtt ||
  4194. vm == &dev_priv->mm.aliasing_ppgtt->base)
  4195. vm = &dev_priv->gtt.base;
  4196. BUG_ON(list_empty(&o->vma_list));
  4197. list_for_each_entry(vma, &o->vma_list, vma_link)
  4198. if (vma->vm == vm)
  4199. return vma->node.size;
  4200. return 0;
  4201. }
  4202. static unsigned long
  4203. i915_gem_shrinker_scan(struct shrinker *shrinker, struct shrink_control *sc)
  4204. {
  4205. struct drm_i915_private *dev_priv =
  4206. container_of(shrinker, struct drm_i915_private, mm.shrinker);
  4207. struct drm_device *dev = dev_priv->dev;
  4208. unsigned long freed;
  4209. bool unlock;
  4210. if (!i915_gem_shrinker_lock(dev, &unlock))
  4211. return SHRINK_STOP;
  4212. freed = i915_gem_purge(dev_priv, sc->nr_to_scan);
  4213. if (freed < sc->nr_to_scan)
  4214. freed += __i915_gem_shrink(dev_priv,
  4215. sc->nr_to_scan - freed,
  4216. false);
  4217. if (unlock)
  4218. mutex_unlock(&dev->struct_mutex);
  4219. return freed;
  4220. }
  4221. static int
  4222. i915_gem_shrinker_oom(struct notifier_block *nb, unsigned long event, void *ptr)
  4223. {
  4224. struct drm_i915_private *dev_priv =
  4225. container_of(nb, struct drm_i915_private, mm.oom_notifier);
  4226. struct drm_device *dev = dev_priv->dev;
  4227. struct drm_i915_gem_object *obj;
  4228. unsigned long timeout = msecs_to_jiffies(5000) + 1;
  4229. unsigned long pinned, bound, unbound, freed;
  4230. bool was_interruptible;
  4231. bool unlock;
  4232. while (!i915_gem_shrinker_lock(dev, &unlock) && --timeout)
  4233. schedule_timeout_killable(1);
  4234. if (timeout == 0) {
  4235. pr_err("Unable to purge GPU memory due lock contention.\n");
  4236. return NOTIFY_DONE;
  4237. }
  4238. was_interruptible = dev_priv->mm.interruptible;
  4239. dev_priv->mm.interruptible = false;
  4240. freed = i915_gem_shrink_all(dev_priv);
  4241. dev_priv->mm.interruptible = was_interruptible;
  4242. /* Because we may be allocating inside our own driver, we cannot
  4243. * assert that there are no objects with pinned pages that are not
  4244. * being pointed to by hardware.
  4245. */
  4246. unbound = bound = pinned = 0;
  4247. list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list) {
  4248. if (!obj->base.filp) /* not backed by a freeable object */
  4249. continue;
  4250. if (obj->pages_pin_count)
  4251. pinned += obj->base.size;
  4252. else
  4253. unbound += obj->base.size;
  4254. }
  4255. list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list) {
  4256. if (!obj->base.filp)
  4257. continue;
  4258. if (obj->pages_pin_count)
  4259. pinned += obj->base.size;
  4260. else
  4261. bound += obj->base.size;
  4262. }
  4263. if (unlock)
  4264. mutex_unlock(&dev->struct_mutex);
  4265. pr_info("Purging GPU memory, %lu bytes freed, %lu bytes still pinned.\n",
  4266. freed, pinned);
  4267. if (unbound || bound)
  4268. pr_err("%lu and %lu bytes still available in the "
  4269. "bound and unbound GPU page lists.\n",
  4270. bound, unbound);
  4271. *(unsigned long *)ptr += freed;
  4272. return NOTIFY_DONE;
  4273. }
  4274. struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
  4275. {
  4276. struct i915_vma *vma;
  4277. /* This WARN has probably outlived its usefulness (callers already
  4278. * WARN if they don't find the GGTT vma they expect). When removing,
  4279. * remember to remove the pre-check in is_pin_display() as well */
  4280. if (WARN_ON(list_empty(&obj->vma_list)))
  4281. return NULL;
  4282. vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link);
  4283. if (vma->vm != obj_to_ggtt(obj))
  4284. return NULL;
  4285. return vma;
  4286. }